-
Notifications
You must be signed in to change notification settings - Fork 3
/
mummerplot
executable file
·1602 lines (1317 loc) · 48.6 KB
/
mummerplot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/opt/local/bin/perl
################################################################################
# Programmer: Adam M Phillippy, The Institute for Genomic Research
# File: mummerplot
# Date: 01 / 08 / 03
# 01 / 06 / 05 rewritten (v3.0)
#
# Usage:
# mummerplot [options] <match file>
#
# Try 'mummerplot -h' for more information.
#
# Purpose: To generate a gnuplot plot for the display of mummer, nucmer,
# promer, and show-tiling alignments.
#
################################################################################
use lib "/Users/garviz/Downloads/MUMmer3.23/scripts";
use Foundation;
use strict;
use IO::Socket;
my $BIN_DIR = "/Users/garviz/Downloads/MUMmer3.23";
my $SCRIPT_DIR = "/Users/garviz/Downloads/MUMmer3.23/scripts";
#================================================================= Globals ====#
#-- terminal types
my $X11 = "x11";
my $PS = "postscript";
my $PNG = "png";
#-- terminal sizes
my $SMALL = "small";
my $MEDIUM = "medium";
my $LARGE = "large";
my %TERMSIZE =
(
$X11 => { $SMALL => 500, $MEDIUM => 700, $LARGE => 900 }, # screen pix
$PS => { $SMALL => 1, $MEDIUM => 2, $LARGE => 3 }, # pages
$PNG => { $SMALL => 800, $MEDIUM => 1024, $LARGE => 1400 } # image pix
);
#-- terminal format
my $FFACE = "Courier";
my $FSIZE = "8";
my $TFORMAT = "%.0f";
my $MFORMAT = "[%.0f, %.0f]";
#-- output suffixes
my $FILTER = "filter";
my $FWDPLOT = "fplot";
my $REVPLOT = "rplot";
my $HLTPLOT = "hplot";
my $GNUPLOT = "gnuplot";
my %SUFFIX =
(
$FILTER => ".filter",
$FWDPLOT => ".fplot",
$REVPLOT => ".rplot",
$HLTPLOT => ".hplot",
$GNUPLOT => ".gp",
$PS => ".ps",
$PNG => ".png"
);
#================================================================= Options ====#
my $OPT_breaklen; # -b option
my $OPT_color; # --[no]color option
my $OPT_coverage; # --[no]coverage option
my $OPT_filter; # -f option
my $OPT_layout; # -l option
my $OPT_prefix = "out"; # -p option
my $OPT_rv; # --rv option
my $OPT_terminal = $X11; # -t option
my $OPT_IdR; # -r option
my $OPT_IdQ; # -q option
my $OPT_IDRfile; # -R option
my $OPT_IDQfile; # -Q option
my $OPT_rport; # -rport option
my $OPT_qport; # -qport option
my $OPT_size = $SMALL; # -small, -medium, -large
my $OPT_SNP; # -S option
my $OPT_xrange; # -x option
my $OPT_yrange; # -y option
my $OPT_title; # -title option
my $OPT_Mfile; # match file
my $OPT_Dfile; # delta filter file
my $OPT_Ffile; # .fplot output
my $OPT_Rfile; # .rplot output
my $OPT_Hfile; # .hplot output
my $OPT_Gfile; # .gp output
my $OPT_Pfile; # .ps .png output
my $OPT_gpstatus; # gnuplot status
my $OPT_ONLY_USE_FATTEST; # Only use fattest alignment for layout
#============================================================== Foundation ====#
my $VERSION = '3.5';
my $USAGE = qq~
USAGE: mummerplot [options] <match file>
~;
my $HELP = qq~
USAGE: mummerplot [options] <match file>
DESCRIPTION:
mummerplot generates plots of alignment data produced by mummer, nucmer,
promer or show-tiling by using the GNU gnuplot utility. After generating
the appropriate scripts and datafiles, mummerplot will attempt to run
gnuplot to generate the plot. If this attempt fails, a warning will be
output and the resulting .gp and .[frh]plot files will remain so that the
user may run gnuplot independently. If the attempt succeeds, either an x11
window will be spawned or an additional output file will be generated
(.ps or .png depending on the selected terminal). Feel free to edit the
resulting gnuplot script (.gp) and rerun gnuplot to change line thinkness,
labels, colors, plot size etc.
MANDATORY:
match file Set the alignment input to 'match file'
Valid inputs are from mummer, nucmer, promer and
show-tiling (.out, .cluster, .delta and .tiling)
OPTIONS:
-b|breaklen Highlight alignments with breakpoints further than
breaklen nucleotides from the nearest sequence end
--[no]color Color plot lines with a percent similarity gradient or
turn off all plot color (default color by match dir)
If the plot is very sparse, edit the .gp script to plot
with 'linespoints' instead of 'lines'
-c
--[no]coverage Generate a reference coverage plot (default for .tiling)
--depend Print the dependency information and exit
-f
--filter Only display .delta alignments which represent the "best"
hit to any particular spot on either sequence, i.e. a
one-to-one mapping of reference and query subsequences
-h
--help Display help information and exit
-l
--layout Layout a .delta multiplot in an intelligible fashion,
this option requires the -R -Q options
--fat Layout sequences using fattest alignment only
-p|prefix Set the prefix of the output files (default '$OPT_prefix')
-rv Reverse video for x11 plots
-r|IdR Plot a particular reference sequence ID on the X-axis
-q|IdQ Plot a particular query sequence ID on the Y-axis
-R|Rfile Plot an ordered set of reference sequences from Rfile
-Q|Qfile Plot an ordered set of query sequences from Qfile
Rfile/Qfile Can either be the original DNA multi-FastA
files or lists of sequence IDs, lens and dirs [ /+/-]
-r|rport Specify the port to send reference ID and position on
mouse double click in X11 plot window
-q|qport Specify the port to send query IDs and position on mouse
double click in X11 plot window
-s|size Set the output size to small, medium or large
--small --medium --large (default '$OPT_size')
-S
--SNP Highlight SNP locations in each alignment
-t|terminal Set the output terminal to x11, postscript or png
--x11 --postscript --png (default '$OPT_terminal')
-t|title Specify the gnuplot plot title (default none)
-x|xrange Set the xrange for the plot '[min:max]'
-y|yrange Set the yrange for the plot '[min:max]'
-V
--version Display the version information and exit
~;
my @DEPEND =
(
"$SCRIPT_DIR/Foundation.pm",
"$BIN_DIR/delta-filter",
"$BIN_DIR/show-coords",
"$BIN_DIR/show-snps",
"gnuplot"
);
my $tigr = new TIGR::Foundation
or die "ERROR: TIGR::Foundation could not be initialized\n";
$tigr -> setVersionInfo ($VERSION);
$tigr -> setUsageInfo ($USAGE);
$tigr -> setHelpInfo ($HELP);
$tigr -> addDependInfo (@DEPEND);
#=========================================================== Function Decs ====#
sub GetParseFunc( );
sub ParseIDs($$);
sub ParseDelta($);
sub ParseCluster($);
sub ParseMummer($);
sub ParseTiling($);
sub LayoutIDs($$);
sub SpanXwY ($$$$$);
sub PlotData($$$);
sub WriteGP($$);
sub RunGP( );
sub ListenGP($$);
sub ParseOptions( );
#=========================================================== Function Defs ====#
MAIN:
{
my @aligns; # (sR eR sQ eQ sim lenR lenQ idR idQ)
my %refs; # (id => (off, len, [1/-1]))
my %qrys; # (id => (off, len, [1/-1]))
#-- Get the command line options (sets OPT_ global vars)
ParseOptions( );
#-- Get the alignment type
my $parsefunc = GetParseFunc( );
if ( $parsefunc != \&ParseDelta &&
($OPT_filter || $OPT_layout || $OPT_SNP) ) {
print STDERR "WARNING: -f -l -S only work with delta input\n";
undef $OPT_filter;
undef $OPT_layout;
undef $OPT_SNP;
}
#-- Parse the reference and query IDs
if ( defined $OPT_IdR ) { $refs{$OPT_IdR} = [ 0, 0, 1 ]; }
elsif ( defined $OPT_IDRfile ) {
ParseIDs ($OPT_IDRfile, \%refs);
}
if ( defined $OPT_IdQ ) { $qrys{$OPT_IdQ} = [ 0, 0, 1 ]; }
elsif ( defined $OPT_IDQfile ) {
ParseIDs ($OPT_IDQfile, \%qrys);
}
#-- Filter the alignments
if ( $OPT_filter || $OPT_layout ) {
print STDERR "Writing filtered delta file $OPT_Dfile\n";
system ("$BIN_DIR/delta-filter -r -q $OPT_Mfile > $OPT_Dfile")
and die "ERROR: Could not run delta-filter, $!\n";
if ( $OPT_filter ) { $OPT_Mfile = $OPT_Dfile; }
}
#-- Parse the alignment data
$parsefunc->(\@aligns);
#-- Layout the alignment data if requested
if ( $OPT_layout ) {
if ( scalar (keys %refs) || scalar (keys %qrys) ) {
LayoutIDs (\%refs, \%qrys);
}
else {
print STDERR "WARNING: --layout option only works with -R or -Q\n";
undef $OPT_layout;
}
}
#-- Plot the alignment data
PlotData (\@aligns, \%refs, \%qrys);
#-- Write the gnuplot script
WriteGP (\%refs, \%qrys);
#-- Run gnuplot script and fork a clipboard listener
unless ( $OPT_gpstatus == -1 ) {
my $child = 1;
if ( $OPT_gpstatus == 0 && $OPT_terminal eq $X11 ) {
print STDERR "Forking mouse listener\n";
$child = fork;
}
#-- parent runs gnuplot
if ( $child ) {
RunGP( );
kill 1, $child;
}
#-- child listens to clipboard
elsif ( defined $child ) {
ListenGP(\%refs, \%qrys);
}
else {
print STDERR "WARNING: Could not fork mouse listener\n";
}
}
exit (0);
}
#------------------------------------------------------------ GetParseFunc ----#
sub GetParseFunc ( )
{
my $fref;
open (MFILE, "<$OPT_Mfile")
or die "ERROR: Could not open $OPT_Mfile, $!\n";
$_ = <MFILE>;
if ( !defined ) { die "ERROR: Could not read $OPT_Mfile, File is empty\n" }
SWITCH: {
#-- tiling
if ( /^>\S+ \d+ bases/ ) {
$fref = \&ParseTiling;
last SWITCH;
}
#-- mummer
if ( /^> \S+/ ) {
$fref = \&ParseMummer;
last SWITCH;
}
#-- nucmer/promer
if ( /^(\S+) (\S+)/ ) {
if ( ! defined $OPT_IDRfile ) {
$OPT_IDRfile = $1;
}
if ( ! defined $OPT_IDQfile ) {
$OPT_IDQfile = $2;
}
$_ = <MFILE>;
if ( (defined) && (/^NUCMER$/ || /^PROMER$/) ) {
$_ = <MFILE>; # sequence header
$_ = <MFILE>; # alignment header
if ( !defined ) {
$fref = \&ParseDelta;
last SWITCH;
}
elsif ( /^\d+ \d+ \d+ \d+ \d+ \d+ \d+$/ ) {
$fref = \&ParseDelta;
last SWITCH;
}
elsif ( /^[ \-][1-3] [ \-][1-3]$/ ) {
$fref = \&ParseCluster;
last SWITCH;
}
}
}
#-- default
die "ERROR: Could not read $OPT_Mfile, Unrecognized file type\n";
}
close (MFILE)
or print STDERR "WARNING: Trouble closing $OPT_Mfile, $!\n";
return $fref;
}
#---------------------------------------------------------------- ParseIDs ----#
sub ParseIDs ($$)
{
my $file = shift;
my $href = shift;
open (IDFILE, "<$file")
or print STDERR "WARNING: Could not open $file, $!\n";
my $dir;
my $aref;
my $isfasta;
my $offset = 0;
while ( <IDFILE> ) {
#-- Ignore blank lines
if ( /^\s*$/ ) { next; }
#-- FastA header
if ( /^>(\S+)/ ) {
if ( exists $href->{$1} ) {
print STDERR "WARNING: Duplicate sequence '$1' ignored\n";
undef $aref;
next;
}
if ( !$isfasta ) { $isfasta = 1; }
if ( defined $aref ) { $offset += $aref->[1] - 1; }
$aref = [ $offset, 0, 1 ];
$href->{$1} = $aref;
next;
}
#-- FastA sequence
if ( $isfasta && /^\S+$/ ) {
if ( defined $aref ) { $aref->[1] += (length) - 1; }
next;
}
#-- ID len dir
if ( !$isfasta && /^(\S+)\s+(\d+)\s+([+-]?)$/ ) {
if ( exists $href->{$1} ) {
print STDERR "WARNING: Duplicate sequence '$1' ignored\n";
undef $aref;
next;
}
$dir = (defined $3 && $3 eq "-") ? -1 : 1;
$aref = [ $offset, $2, $dir ];
$offset += $2 - 1;
$href->{$1} = $aref;
next;
}
#-- default
print STDERR "WARNING: Could not parse $file\n$_";
undef %$href;
last;
}
close (IDFILE)
or print STDERR "WARNING: Trouble closing $file, $!\n";
}
#-------------------------------------------------------------- ParseDelta ----#
sub ParseDelta ($)
{
my $aref = shift;
print STDERR "Reading delta file $OPT_Mfile\n";
open (MFILE, "<$OPT_Mfile")
or die "ERROR: Could not open $OPT_Mfile, $!\n";
my @align;
my $ispromer;
my ($sim, $tot);
my ($lenR, $lenQ, $idR, $idQ);
$_ = <MFILE>;
$_ = <MFILE>;
$ispromer = /^PROMER/;
while ( <MFILE> ) {
#-- delta int
if ( /^([-]?\d+)$/ ) {
if ( $1 < 0 ) {
$tot ++;
}
elsif ( $1 == 0 ) {
$align[4] = ($tot - $sim) / $tot * 100.0;
push @$aref, [ @align ];
$tot = 0;
}
next;
}
#-- alignment header
if ( /^(\d+) (\d+) (\d+) (\d+) \d+ (\d+) \d+$/ ) {
if ( $tot == 0 ) {
@align = ($1, $2, $3, $4, 0, $lenR, $lenQ, $idR, $idQ);
$tot = abs($1 - $2) + 1;
$sim = $5;
if ( $ispromer ) { $tot /= 3.0; }
next;
}
#-- drop to default
}
#-- sequence header
if ( /^>(\S+) (\S+) (\d+) (\d+)$/ ) {
($idR, $idQ, $lenR, $lenQ) = ($1, $2, $3, $4);
$tot = 0;
next;
}
#-- default
die "ERROR: Could not parse $OPT_Mfile\n$_";
}
close (MFILE)
or print STDERR "WARNING: Trouble closing $OPT_Mfile, $!\n";
}
#------------------------------------------------------------ ParseCluster ----#
sub ParseCluster ($)
{
my $aref = shift;
print STDERR "Reading cluster file $OPT_Mfile\n";
open (MFILE, "<$OPT_Mfile")
or die "ERROR: Could not open $OPT_Mfile, $!\n";
my @align;
my ($dR, $dQ, $len);
my ($lenR, $lenQ, $idR, $idQ);
$_ = <MFILE>;
$_ = <MFILE>;
while ( <MFILE> ) {
#-- match
if ( /^\s+(\d+)\s+(\d+)\s+(\d+)\s+\S+\s+\S+$/ ) {
@align = ($1, $1, $2, $2, 100, $lenR, $lenQ, $idR, $idQ);
$len = $3 - 1;
$align[1] += $dR == 1 ? $len : -$len;
$align[3] += $dQ == 1 ? $len : -$len;
push @$aref, [ @align ];
next;
}
#-- cluster header
if ( /^[ \-][1-3] [ \-][1-3]$/ ) {
$dR = /^-/ ? -1 : 1;
$dQ = /-[1-3]$/ ? -1 : 1;
next;
}
#-- sequence header
if ( /^>(\S+) (\S+) (\d+) (\d+)$/ ) {
($idR, $idQ, $lenR, $lenQ) = ($1, $2, $3, $4);
next;
}
#-- default
die "ERROR: Could not parse $OPT_Mfile\n$_";
}
close (MFILE)
or print STDERR "WARNING: Trouble closing $OPT_Mfile, $!\n";
}
#------------------------------------------------------------- ParseMummer ----#
sub ParseMummer ($)
{
my $aref = shift;
print STDERR "Reading mummer file $OPT_Mfile (use mummer -c)\n";
open (MFILE, "<$OPT_Mfile")
or die "ERROR: Could not open $OPT_Mfile, $!\n";
my @align;
my ($dQ, $len);
my ($lenQ, $idQ);
while ( <MFILE> ) {
#-- 3 column match
if ( /^\s+(\d+)\s+(\d+)\s+(\d+)$/ ) {
@align = ($1, $1, $2, $2, 100, 0, $lenQ, "REF", $idQ);
$len = $3 - 1;
$align[1] += $len;
$align[3] += $dQ == 1 ? $len : -$len;
push @$aref, [ @align ];
next;
}
#-- 4 column match
if ( /^\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)$/ ) {
@align = ($2, $2, $3, $3, 100, 0, $lenQ, $1, $idQ);
$len = $4 - 1;
$align[1] += $len;
$align[3] += $dQ == 1 ? $len : -$len;
push @$aref, [ @align ];
next;
}
#-- sequence header
if ( /^> (\S+)/ ) {
$idQ = $1;
$dQ = /^> \S+ Reverse/ ? -1 : 1;
$lenQ = /Len = (\d+)/ ? $1 : 0;
next;
}
#-- default
die "ERROR: Could not parse $OPT_Mfile\n$_";
}
close (MFILE)
or print STDERR "WARNING: Trouble closing $OPT_Mfile, $!\n";
}
#------------------------------------------------------------- ParseTiling ----#
sub ParseTiling ($)
{
my $aref = shift;
print STDERR "Reading tiling file $OPT_Mfile\n";
open (MFILE, "<$OPT_Mfile")
or die "ERROR: Could not open $OPT_Mfile, $!\n";
my @align;
my ($dR, $dQ, $len);
my ($lenR, $lenQ, $idR, $idQ);
while ( <MFILE> ) {
#-- tile
if ( /^(\S+)\s+\S+\s+\S+\s+(\d+)\s+\S+\s+(\S+)\s+([+-])\s+(\S+)$/ ) {
@align = ($1, $1, 1, 1, $3, $lenR, $2, $idR, $5);
$len = $2 - 1;
$align[1] += $len;
$align[($4 eq "-" ? 2 : 3)] += $len;
push @$aref, [ @align ];
next;
}
#-- sequence header
if ( /^>(\S+) (\d+) bases$/ ) {
($idR, $lenR) = ($1, $2);
next;
}
#-- default
die "ERROR: Could not parse $OPT_Mfile\n$_";
}
close (MFILE)
or print STDERR "WARNING: Trouble closing $OPT_Mfile, $!\n";
if ( ! defined $OPT_coverage ) { $OPT_coverage = 1; }
}
#--------------------------------------------------------------- LayoutIDs ----#
# For each reference and query sequence, find the set of alignments that
# produce the heaviest (both in non-redundant coverage and percent
# identity) alignment subset of each sequence using a modified version
# of the longest increasing subset algorithm. Let R be the union of all
# reference LIS subsets, and Q be the union of all query LIS
# subsets. Let S be the intersection of R and Q. Using this LIS subset,
# recursively span reference and query sequences by their smaller
# counterparts until all spanning sequences have been placed. The goal
# is to cluster all the "major" alignment information along the main
# diagonal for easy viewing and interpretation.
sub LayoutIDs ($$)
{
my $rref = shift;
my $qref = shift;
my %rc; # chains of qry seqs needed to span each ref
my %qc; # chains of ref seqs needed to span each qry
# {idR} -> [ placed, len, {idQ} -> [ \slope, \loR, \hiR, \loQ, \hiQ ] ]
# {idQ} -> [ placed, len, {idR} -> [ \slope, \loQ, \hiQ, \loR, \hiR ] ]
my @rl; # oo of ref seqs
my @ql; # oo of qry seqs
# [ [idR, slope] ]
# [ [idQ, slope] ]
#-- get the filtered alignments
open (BTAB, "$BIN_DIR/show-coords -B $OPT_Dfile |")
or die "ERROR: Could not open show-coords pipe, $!\n";
my @align;
my ($sR, $eR, $sQ, $eQ, $lenR, $lenQ, $idR, $idQ);
my ($loR, $hiR, $loQ, $hiQ);
my ($dR, $dQ, $slope);
while ( <BTAB> ) {
chomp;
@align = split "\t";
if ( scalar @align != 21 ) {
die "ERROR: Could not read show-coords pipe, invalid btab format\n";
}
$sR = $align[8]; $eR = $align[9];
$sQ = $align[6]; $eQ = $align[7];
$lenR = $align[18]; $lenQ = $align[2];
$idR = $align[5]; $idQ = $align[0];
#-- skip it if not on include list
if ( !exists $rref->{$idR} || !exists $qref->{$idQ} ) { next; }
#-- get orientation of both alignments and alignment slope
$dR = $sR < $eR ? 1 : -1;
$dQ = $sQ < $eQ ? 1 : -1;
$slope = $dR == $dQ ? 1 : -1;
#-- get lo's and hi's
$loR = $dR == 1 ? $sR : $eR;
$hiR = $dR == 1 ? $eR : $sR;
$loQ = $dQ == 1 ? $sQ : $eQ;
$hiQ = $dQ == 1 ? $eQ : $sQ;
if ($OPT_ONLY_USE_FATTEST)
{
#-- Check to see if there is another better alignment
if (exists $qc{$idQ})
{
my ($oldR) = keys %{$qc{$idQ}[2]};
my $val = $qc{$idQ}[2]{$oldR};
if (${$val->[4]} - ${$val->[3]} > $hiR - $loR)
{
#-- Old alignment is better, skip this one
next;
}
else
{
#-- This alignment is better, prune old alignment
delete $rc{$oldR}[2]{$idQ};
delete $qc{$idQ};
}
}
}
#-- initialize
if ( !exists $rc{$idR} ) { $rc{$idR} = [ 0, $lenR, { } ]; }
if ( !exists $qc{$idQ} ) { $qc{$idQ} = [ 0, $lenQ, { } ]; }
#-- if no alignments for these two exist OR
#-- this alignment is bigger than the current
if ( !exists $rc{$idR}[2]{$idQ} || !exists $qc{$idQ}[2]{$idR} ||
$hiR - $loR >
${$rc{$idR}[2]{$idQ}[2]} - ${$rc{$idR}[2]{$idQ}[1]} ) {
#-- rc and qc reference these anonymous values
my $aref = [ $slope, $loR, $hiR, $loQ, $hiQ ];
#-- rc is ordered [ slope, loR, hiR, loQ, hiQ ]
#-- qc is ordered [ slope, loQ, hiQ, loR, hiR ]
$rc{$idR}[2]{$idQ}[0] = $qc{$idQ}[2]{$idR}[0] = \$aref->[0];
$rc{$idR}[2]{$idQ}[1] = $qc{$idQ}[2]{$idR}[3] = \$aref->[1];
$rc{$idR}[2]{$idQ}[2] = $qc{$idQ}[2]{$idR}[4] = \$aref->[2];
$rc{$idR}[2]{$idQ}[3] = $qc{$idQ}[2]{$idR}[1] = \$aref->[3];
$rc{$idR}[2]{$idQ}[4] = $qc{$idQ}[2]{$idR}[2] = \$aref->[4];
}
}
close (BTAB)
or print STDERR "WARNING: Trouble closing show-coords pipe, $!\n";
#-- recursively span sequences to generate the layout
foreach $idR ( sort { $rc{$b}[1] <=> $rc{$a}[1] } keys %rc ) {
SpanXwY ($idR, \%rc, \@rl, \%qc, \@ql);
}
#-- undefine the current offsets
foreach $idR ( keys %{$rref} ) { undef $rref->{$idR}[0]; }
foreach $idQ ( keys %{$qref} ) { undef $qref->{$idQ}[0]; }
#-- redefine the offsets according to the new layout
my $roff = 0;
foreach my $r ( @rl ) {
$idR = $r->[0];
$rref->{$idR}[0] = $roff;
$rref->{$idR}[2] = $r->[1];
$roff += $rref->{$idR}[1] - 1;
}
#-- append the guys left out of the layout
foreach $idR ( keys %{$rref} ) {
if ( !defined $rref->{$idR}[0] ) {
$rref->{$idR}[0] = $roff;
$roff += $rref->{$idR}[1] - 1;
}
}
#-- redefine the offsets according to the new layout
my $qoff = 0;
foreach my $q ( @ql ) {
$idQ = $q->[0];
$qref->{$idQ}[0] = $qoff;
$qref->{$idQ}[2] = $q->[1];
$qoff += $qref->{$idQ}[1] - 1;
}
#-- append the guys left out of the layout
foreach $idQ ( keys %{$qref} ) {
if ( !defined $qref->{$idQ}[0] ) {
$qref->{$idQ}[0] = $qoff;
$qoff += $qref->{$idQ}[1] - 1;
}
}
}
#----------------------------------------------------------------- SpanXwY ----#
sub SpanXwY ($$$$$) {
my $x = shift; # idX
my $xcr = shift; # xc ref
my $xlr = shift; # xl ref
my $ycr = shift; # yc ref
my $ylr = shift; # yl ref
my @post;
foreach my $y ( sort { ${$xcr->{$x}[2]{$a}[1]} <=> ${$xcr->{$x}[2]{$b}[1]} }
keys %{$xcr->{$x}[2]} ) {
#-- skip if already placed (RECURSION BASE)
if ( $ycr->{$y}[0] ) { next; }
else { $ycr->{$y}[0] = 1; }
#-- get len and slope info for y
my $len = $ycr->{$y}[1];
my $slope = ${$xcr->{$x}[2]{$y}[0]};
#-- if we need to flip, reverse complement all y records
if ( $slope == -1 ) {
foreach my $xx ( keys %{$ycr->{$y}[2]} ) {
${$ycr->{$y}[2]{$xx}[0]} *= -1;
my $loy = ${$ycr->{$y}[2]{$xx}[1]};
my $hiy = ${$ycr->{$y}[2]{$xx}[2]};
${$ycr->{$y}[2]{$xx}[1]} = $len - $hiy + 1;
${$ycr->{$y}[2]{$xx}[2]} = $len - $loy + 1;
}
}
#-- place y
push @{$ylr}, [ $y, $slope ];
#-- RECURSE if y > x, else save for later
if ( $len > $xcr->{$x}[1] ) { SpanXwY ($y, $ycr, $ylr, $xcr, $xlr); }
else { push @post, $y; }
}
#-- RECURSE for all y < x
foreach my $y ( @post ) { SpanXwY ($y, $ycr, $ylr, $xcr, $xlr); }
}
#---------------------------------------------------------------- PlotData ----#
sub PlotData ($$$)
{
my $aref = shift;
my $rref = shift;
my $qref = shift;
print STDERR "Writing plot files $OPT_Ffile, $OPT_Rfile",
(defined $OPT_Hfile ? ", $OPT_Hfile\n" : "\n");
open (FFILE, ">$OPT_Ffile")
or die "ERROR: Could not open $OPT_Ffile, $!\n";
print FFILE "#-- forward hits sorted by %sim\n0 0 0\n0 0 0\n\n\n";
open (RFILE, ">$OPT_Rfile")
or die "ERROR: Could not open $OPT_Rfile, $!\n";
print RFILE "#-- reverse hits sorted by %sim\n0 0 0\n0 0 0\n\n\n";
if ( defined $OPT_Hfile ) {
open (HFILE, ">$OPT_Hfile")
or die "ERROR: Could not open $OPT_Hfile, $!\n";
print HFILE "#-- highlighted hits sorted by %sim\n0 0 0\n0 0 0\n\n\n";
}
my $fh;
my $align;
my $isplotted;
my $ismultiref;
my $ismultiqry;
my ($plenR, $plenQ, $pidR, $pidQ);
#-- for each alignment sorted by ascending identity
foreach $align ( sort { $a->[4] <=> $b->[4] } @$aref ) {
my ($sR, $eR, $sQ, $eQ, $sim, $lenR, $lenQ, $idR, $idQ) = @$align;
if ( ! defined $pidR ) {
($plenR, $plenQ, $pidR, $pidQ) = ($lenR, $lenQ, $idR, $idQ);
}
#-- set the sequence offset, length, direction, etc...
my ($refoff, $reflen, $refdir);
my ($qryoff, $qrylen, $qrydir);
if ( defined (%$rref) ) {
#-- skip reference sequence or set atts from hash
if ( !exists ($rref->{$idR}) ) { next; }
else { ($refoff, $reflen, $refdir) = @{$rref->{$idR}}; }
}
else {
#-- no reference hash, so default atts
($refoff, $reflen, $refdir) = (0, $lenR, 1);
}
if ( defined (%$qref) ) {
#-- skip query sequence or set atts from hash
if ( !exists ($qref->{$idQ}) ) { next; }
else { ($qryoff, $qrylen, $qrydir) = @{$qref->{$idQ}}; }
}
else {
#-- no query hash, so default atts
($qryoff, $qrylen, $qrydir) = (0, $lenQ, 1);
}
#-- get the orientation right
if ( $refdir == -1 ) {
$sR = $reflen - $sR + 1;
$eR = $reflen - $eR + 1;
}
if ( $qrydir == -1 ) {
$sQ = $qrylen - $sQ + 1;
$eQ = $qrylen - $eQ + 1;
}
#-- forward file, reverse file, highlight file
my @fha;
if ( defined $OPT_breaklen &&
( ($sR - 1 > $OPT_breaklen &&
$sQ - 1 > $OPT_breaklen &&
$reflen - $sR > $OPT_breaklen &&
$qrylen - $sQ > $OPT_breaklen)
||
($eR - 1 > $OPT_breaklen &&
$eQ - 1 > $OPT_breaklen &&
$reflen - $eR > $OPT_breaklen &&
$qrylen - $eQ > $OPT_breaklen) ) ) {
push @fha, \*HFILE;
}
push @fha, (($sR < $eR) == ($sQ < $eQ) ? \*FFILE : \*RFILE);
#-- plot it
$sR += $refoff; $eR += $refoff;
$sQ += $qryoff; $eQ += $qryoff;
if ( $OPT_coverage ) {
foreach $fh ( @fha ) {
print $fh
"$sR 10 $sim\n", "$eR 10 $sim\n\n\n",
"$sR $sim 0\n", "$eR $sim 0\n\n\n";
}
}
else {
foreach $fh ( @fha ) {
print $fh "$sR $sQ $sim\n", "$eR $eQ $sim\n\n\n";
}
}
#-- set some flags
if ( !$ismultiref && $idR ne $pidR ) { $ismultiref = 1; }
if ( !$ismultiqry && $idQ ne $pidQ ) { $ismultiqry = 1; }
if ( !$isplotted ) { $isplotted = 1; }
}
#-- highlight the SNPs
if ( defined $OPT_SNP ) {
print STDERR "Determining SNPs from sequence and alignment data\n";
open (SNPS, "$BIN_DIR/show-snps -H -T -l $OPT_Mfile |")
or die "ERROR: Could not open show-snps pipe, $!\n";
my @snps;
my ($pR, $pQ, $lenR, $lenQ, $idR, $idQ);
while ( <SNPS> ) {
chomp;
@snps = split "\t";
if ( scalar @snps != 14 ) {
die "ERROR: Could not read show-snps pipe, invalid format\n";
}
$pR = $snps[0]; $pQ = $snps[3];
$lenR = $snps[8]; $lenQ = $snps[9];
$idR = $snps[12]; $idQ = $snps[13];
#-- set the sequence offset, length, direction, etc...
my ($refoff, $reflen, $refdir);
my ($qryoff, $qrylen, $qrydir);
if ( defined (%$rref) ) {
#-- skip reference sequence or set atts from hash
if ( !exists ($rref->{$idR}) ) { next; }
else { ($refoff, $reflen, $refdir) = @{$rref->{$idR}}; }
}
else {
#-- no reference hash, so default atts
($refoff, $reflen, $refdir) = (0, $lenR, 1);
}
if ( defined (%$qref) ) {
#-- skip query sequence or set atts from hash
if ( !exists ($qref->{$idQ}) ) { next; }
else { ($qryoff, $qrylen, $qrydir) = @{$qref->{$idQ}}; }
}
else {
#-- no query hash, so default atts
($qryoff, $qrylen, $qrydir) = (0, $lenQ, 1);
}