-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmapdraw.pl
executable file
·1458 lines (1279 loc) · 45.3 KB
/
mapdraw.pl
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
#!/usr/bin/perl -w
use PDF::API2;
use PDF::Table;
use Math::Trig;
use lib ".";
use DMAP::Assembly;
use strict;
=pod
=head mapdraw.pl
draws a genetic map aligned to a physical map.
=head2 file format
MARKER,marker_name,marker_position,pseudomolecule_position,markertype
MOLECULE,molecule_name,start,length
LINK,type,colourname,linetype(dash|solid),font
options:
-infile input file
-outfile output file
-height figure height in mm
-width figure width in mm
-linewidth width of drawn lines in points
-colour (multi) name:red,green,blue values 0-255
-chrom chromosome colour
-mol molecule colour
-minfont minimum font size for markers (default 4)
-crowd try to fit more marker labels in (default 2.5 - higher is more labels)
-plotallmap plot every genetic map pos rather than binning.
-nocount Don't include marker count.
-nolcount Don't count physically colocated labels.
-genbin resolution of genetic map bin (default 0.1cM)
-nosize don't add molecule size to the plot title.
-seqbin resolution of sequence marker bin (only one marker of each type will be shown per bin) Default 100.
-title plot title.
-unanchored Plot unanchored markers as grey bars on genetic map
-trim Trim genetic map to maximum map position (otherwise 100 or max pos if greater). If -unanchored is not set then it trims to anchored markers only.
Colours are X11/HTML/CSS colours.
Font is Helvetica or Times with options Roman Bold or Italic e.g. Helvetica Roman
=cut
#page definitions/resolution
# PDF::API2 native resolution is in points. This can be redefined into internal units.
use constant RES => 72/300;
use constant mm => 25.4 / 72;
use constant in => 1 / 72;
use constant pt => 1;
use Getopt::Long;
my $filename="drawing.pdf";
my @usercolours=();
my $chrcolour="default";
my $molcolour="default";
my $labelfont="Helvetica";
my $infile="";
my $height=297; #height in mm;
my $width=210; #width in mm
my $chrwidth=10; # chromosome width in mm - min 4
my $molwidth=6; # molecule width in mm - min 4
my $binsize=1.3;
my $chrheight=90; # percent of page height for a chromosome.
my $molfontsize=10;
my $markerfontsize=6;
my $minfont=4;
my $linewidth=0.5;
my $invert=0; # whether to have 0 at the bottom or top(default)
my $lineanglespace=4;
my $markerlabelwidth=32;
my $plotallmap=0;
my $crowd=2.5;
my $plotstart=0; # limits for physical molecule range to draw (0 no limit)
my $plotend=0;# limits for physical molecule range to draw (0 no limit)
my $maplabelwidth=15;
my $mollabelsize=55;
my $nosize=0;
my $molposbin=0;
my $plottitle="Chromosome Map";
my $genbin="0.1";
my $nocount=0;
my $nolcount=0;
my $debug=0;
my $correlate=""; # draw a correlation between the genetic and physical maps to the given file.
my $agpfile=""; # AGP file containing molecule definition.
my $gfffile=""; #GFF file containing marker positions on the molecules.
my $mapfile=""; #file containing marker positions on the molecules. Format "markername position .*"
my $mapname="default"; #name of map (for versioning etc.)
my $chrtrim=0; # trim chromosome to map limits
my $plotunanchored=0; # plot unanchored linkage markers.
GetOptions(
"infile=s"=>\$infile,
"agpfile=s"=>\$agpfile,
"gfffile=s"=>\$gfffile,
"mapfile=s"=>\$mapfile,
"mapname=s"=>\$mapname,
"outfile=s"=>\$filename,
"chrheight=i"=>\$chrheight,
"height=i"=>\$height,
"width=i"=>\$width,
"invert"=>\$invert,
"plotstart=i"=>\$plotstart,
"plotend=i"=>\$plotend,
"nosize"=>\$nosize,
"plotallmap"=>\$plotallmap,
"crowd=f"=>\$crowd,
"seqbin=i"=>\$molposbin,
"title=s"=>\$plottitle,
"chrwidth=i"=>\$chrwidth,
"colour=s"=>\@usercolours,
"chrom=s"=>\$chrcolour,
"correlation=s"=>\$correlate,
"genbin=s"=>\$genbin,
"minfont=i"=>\$minfont,
"nocount"=>\$nocount,
"nolcount"=>\$nolcount,
"linewidth=f"=>\$linewidth,
"mol=s"=>\$molcolour,
"unanchored"=>\$plotunanchored,
"trim"=>\$chrtrim,
"debug"=>\$debug
);
my $mapdp=0;
if (index($genbin,".")>=0){
$mapdp=length($genbin)-index($genbin,".")-1;
}
#print STDERR join("\t",$mapdp, length($genbin), index($genbin,".")),"\n";
if ($molcolour eq 'default'){ $molcolour='black';}
if ($chrcolour eq 'default'){ $chrcolour='black';}
unless (-e $infile || ($agpfile && -e $agpfile && $mapfile && -e $mapfile && $gfffile && -e $gfffile)) {
die "cannot find input file $infile\n";
}
my @markers=(); # list of markers to plot
my %colours=();
my %links=();
my @molecules=();
my $maxchrpos=0; # maximum genetic map distance
my $maxmolpos=0; # maximum physical map distance
my $minchrpos=1000;
my $assembly=DMAP::Assembly->new({name=>$plottitle});
my $mapfound=0;
my $agpfound=0;
my $gfffound=0;
my @unanchored=(); # holds unanchored genetic markers.
if ($agpfile && -e $agpfile) {
open AGPFILE, "$agpfile" or die "Could not open AGP file: $!\n";
my $mollen=0;
while (my $agpline=<AGPFILE>){
chomp $agpline;
my @F=split /\t/,$agpline;
next unless scalar @F >5;
if ($F[4]=~m/[UN]/){
$mollen+=$F[5];
} else {
my $wc=$F[8]eq'-'?1:0;
my $fraglen=1+$F[7]-$F[6];
$assembly->addMolecule($mollen+1,$fraglen,$F[5],$wc,$F[6]-1);
print STDERR "adding molecule $F[7] $F[5]\n";
$mollen+=$fraglen;
}
}
close AGPFILE;
$agpfound= $mollen;
$maxmolpos=$mollen;
}
if ($gfffile && -e $gfffile) {
open GFFFILE, "$gfffile" or die "Could not open GFF file: $!\n";
my $gmap="default";
if ($mapname && ! $mapfile) {
$gmap=$mapname;
}
while (my $gff=<GFFFILE>){
next if $gff=~m/^!/;
chomp $gff;
my @F=split / *\t */, $gff;
my %ext=();
unless ($F[8]){
$F[8]=$F[7];
}
if ($F[8]){
foreach my $n (split (/; */ , $F[8])){
my ($key, $value)= split( / *= */,$n);
$key=~s/^ *//;
$value=~s/^ *//;
chomp $value;
$ext{$key}=$value;
}
}else{
warn "Error with GFF line: $gff\n";
}
my %notes=();
if (exists($ext{"Note"})){
#set default $notes{pos}
#$notes{pos}=-1;
foreach my $n (split(/, /, $ext{'Note'})){
my ($key,$value)= split(/: */, $n);
$key=~s/^ *//;
$value=~s/^ *//;
chomp $value;
$notes{$key}=$value;
}
}
my $mmap=$gmap;
#if ($notes{pos}==-1){ $mmap="fake";}
if ($debug) {
foreach my $e (keys %ext){
print STDERR ":$e:$ext{$e}:\n";
}
foreach my $n (keys %notes){
print STDERR ":$n:$notes{$n}:\n";
}
}
unless (exists($ext{ID}) #and exists($notes{pos})
and exists($notes{type})){
print STDERR "badly specified GFF line - need ID and Note:type in $F[8]\n";
next;
}
# $assembly->addGFFMarker($F[0], $ext{ID},$notes{pos}, $F[3],$notes{type}, $mmap);
$assembly->addGFFMarker($F[0], $ext{ID}, $F[3],$notes{type});
if (exists($notes{pos}) && ! $mapfile ){
$assembly->addMapPos($mmap,$ext{ID}, $notes{pos});
}
# $markername, $geneticmapposition, $physicalpositioninmolecule, $type);
}
$gfffound=1;
close GFFFILE;
}
if ($mapfile && -e $mapfile) {
my $gmap=$mapname?$mapname:"default";
open (MAPFILE, "$mapfile") or die "Cannot open map file $mapfile: $!\n";
while (my $mapline=<MAPFILE>){
next if $mapline=~m/^ *$/; #skip blank lines and comments.
next if $mapline=~m/^;/; #skip blank lines and comments.
next unless $mapline=~m/;/;
#split lines and add to assembly
my ($markername, $markerpos, $junk)= split /\s+/, $mapline, 3;
print STDERR " reading marker $markername at $markerpos\n";
if ($assembly->addMapPos($gmap, $markername, $markerpos)){
if ($markerpos > $maxchrpos){$maxchrpos=$markerpos;}
if ($markerpos < $minchrpos){$minchrpos=$markerpos;}
print STDERR "ADDING MAP POS $markername $markerpos $maxchrpos $minchrpos\n";
} else {
if ($plotunanchored){
push @unanchored, {"name"=>$markername, "pos"=>$markerpos};
if ($markerpos > $maxchrpos){$maxchrpos=$markerpos;}
if ($markerpos < $minchrpos){$minchrpos=$markerpos;}
}
}
}
close MAPFILE;
$mapfound=1;
}
if ($mapfound && $agpfound && $gfffound){
print STDERR "MAXCHR: $maxchrpos\n";
# need to build marker and molecule lists.
#molecules
foreach my $m ($assembly->getAllMolecules()) {
#if (exists($m->{molecule}->{markers}{$m->{name}}{mappos}{$mapname})){
push @molecules, {name=>$m->{name}, start=>$m->{start}, length=>$m->{molecule}->{length} };
#print STDERR "name=>".$m->{name}.", start=>".$m->{start}.", length ".$m->{molecule}->{length}."\n";
#} else {
# warn "Marker ".$m->{name}." not in map $mapname\n";
#}
}
#markers
unless ($mapname){ $mapname="default";}
@markers=$assembly->getMarkers($mapname);
#push @markers, {name=>$f[0], avemappos=>avemappos($f[1]),mappos=>binmappos($f[1]), molpos=>$f[2], type=>$f[3]};
}
open (INFILE, $infile) or die "Error opening input file: $!\n";
while (my $line=<INFILE>){
chomp $line;
my @f=split /,/, $line;
#print STDERR join(":",@f),"\n";
my $type=shift @f;
foreach ($type) {
/^MOLECULE$/ and do {
push @molecules, {name=>$f[0], start=>$f[1], length=>$f[2] };
if ($f[1]+$f[2] > $maxmolpos) {$maxmolpos=$f[1]+$f[2];}
$assembly->addMolecule($f[1], $f[2], $f[0]);
last;};
/^LINK$/ and do { $links{$f[0]}={ colour=>$f[1], line=>$f[2], font=>$f[3]}; last;};
/^MARKER$/ and do {
push @markers, {name=>$f[0], avemappos=>avemappos($f[1]),mappos=>binmappos($f[1]), molpos=>$f[2], type=>$f[3]};
my $mmax=binmappos($f[1]); my $mmin=binmappos($f[1]);
if ($mmax=~/(-?\d+)-(-?\d+)/){$mmax=$2; $mmin=$1}
if ($mmax>$maxchrpos){$maxchrpos=$mmax;}
if ($mmin<$minchrpos){$minchrpos=$mmin;}
$assembly->addMarker($f[0],avemappos($f[1]), $f[2],$f[3]);
last ;};
}
}
close INFILE;
my $report=$assembly->report($mapname);
foreach my $col (@usercolours){
my ($n, $r, $g, $b)=split/[:,]/, $col;
if ($n && $r && $b && $g) {
$colours{$n}={red=>$r, green=>$g, blue=>$b};
}
}
my $maxheight =$chrheight*$height/100;
my $basemargin=$height*(100-$chrheight)/200;
my $chrx=($width/5)-($chrwidth/2);
my $molx=(2*$width/5)-($molwidth/2);
my $mollabelwidth=$width-$mollabelsize;
my $pdf=PDF::API2->new( -file =>$filename);
die "could not create PDF\n" unless $pdf;
# set up font definitions.
my %font = (
Helvetica => {
Bold => $pdf->corefont( 'Helvetica-Bold', -encoding => 'latin1' ),
Roman => $pdf->corefont( 'Helvetica', -encoding => 'latin1' ),
Italic => $pdf->corefont( 'Helvetica-Oblique', -encoding => 'latin1' ),
},
Times => {
Bold => $pdf->corefont( 'Times-Bold', -encoding => 'latin1' ),
Roman => $pdf->corefont( 'Times', -encoding => 'latin1' ),
Italic => $pdf->corefont( 'Times-Italic', -encoding => 'latin1' ),
}
);
my $moltextfont=$font{'Helvetica'}{'Bold'};
my $titletextfont=$font{'Helvetica'}{'Bold'};
my $titlefontsize=14;
my $titlecolour='black';
# define colours here.
#get page for main figure
my $page=$pdf->page;
$page->mediabox(int($width/mm), int($height/mm));
#get page for correlation plot.
my $cpage=$pdf->page;
$cpage->mediabox(int($width/mm), int($height/mm));
# get page for correlation plot with modelled curve.
my $dpage=$pdf->page;
$dpage->mediabox(int($width/mm), int($height/mm));
# get page for tabulated reports.
my $rpage=$pdf->page;
$rpage->mediabox(int($width/mm), int($height/mm));
my $caxiscolour='black';
my $cbasecolour='darkgray';
# draw a box and title. Plot will be square
#plotsize - box should be about 70% of total available width
my $cplotsize=$width * 0.7;
my $cplotyaxis=$width * 0.2;
my $cplotxaxis=($height-$cplotsize)/2;
my $cplotystart=0<$minchrpos?0:$minchrpos;
my $cplotyscale=($maxchrpos>100?$maxchrpos:100)-$cplotystart;
if ($chrtrim){
$cplotystart=$minchrpos;
$cplotyscale=$maxchrpos-$cplotystart;
}
# plot the figure outline for the correlation and modelled plots.
plotfig($cpage);
plotfig($dpage);
sub plotfig { #plots the figure outline on the given page.
my ($cpage)=@_;
my $cplot=$cpage->gfx;
if ($linewidth){
my $lw=$cplot->linewidth($linewidth);
#foreach my $k (keys %$lw){
# print STDERR "LINEWIDTH: $k $lw->{$k}\n";
#}
}
my $ctext=$cpage->text;
$ctext->font($titletextfont,$titlefontsize/pt);
$ctext->fillcolor($caxiscolour);
$ctext->translate(($cplotyaxis+$cplotsize/2)/mm,($cplotxaxis+$cplotsize+5)/mm);
$ctext->text_center($plottitle." correlation plot");
$cplot->fillcolor($cbasecolour);
$cplot->rect($cplotyaxis/mm,$cplotxaxis/mm,$cplotsize/mm,$cplotsize/mm);
$cplot->fill;
$cplot->strokecolor($caxiscolour);
$cplot->rect(($cplotyaxis-1)/mm,($cplotxaxis-1)/mm,($cplotsize+2)/mm,($cplotsize+2)/mm);
$cplot->stroke;
for my $y (qw/0 10 20 30 40 50 60 70 80 90 100/){
my $cypos=$cplotxaxis+$cplotsize*($y-$cplotystart)/$cplotyscale;
$cplot->move(($cplotyaxis-1)/mm,$cypos/mm);
$cplot->line(($cplotyaxis-4)/mm,$cypos/mm);
$cplot->stroke;
$ctext->font($moltextfont,10/pt);
$ctext->translate(($cplotyaxis-6)/mm,$cypos/mm - 5/pt);
$ctext->fillcolor($caxiscolour);
$ctext->text_right($y);
#add text labels
}
$ctext->font($moltextfont,12/pt);
$ctext->fillcolor($caxiscolour);
#print STDERR join(":",$ctext->textpos),"\n";
$ctext->rotate(90);
#print STDERR join(":",$ctext->textpos),"\n";
$ctext->transform(-translate=>[($cplotyaxis-15)/mm,($cplotxaxis+$cplotsize* 0.5 )/mm],-rotate=>90);
#print STDERR join(":",$ctext->textpos),"\n";
$ctext->text_center("Genetic Map Position (cM)");
# need ten labels but need to scale appropriately.
my $maxmolscale=length("".$maxmolpos); #number of digits.
my $xint="1". "0" x ($maxmolscale-1);
print STDERR "XINT $xint $maxmolpos\n";
while ($maxmolpos/$xint <5) {$xint /=2;}
for (my $xl=0; $xl<$maxmolpos; $xl+=$xint){
my $xmark=$cplotyaxis+$cplotsize*$xl/$maxmolpos;
$cplot->move($xmark/mm, ($cplotxaxis-1)/mm);
$cplot->line($xmark/mm, ($cplotxaxis-4)/mm);
$cplot->stroke;
$ctext->font($moltextfont,10/pt);
$ctext->transform(-translate=>[$xmark/mm - 5/pt, ($cplotxaxis-6)/mm], -rotate=>270);
$ctext->text($xl/1000);
}
$ctext->font($moltextfont,12/pt);
$ctext->translate(($cplotyaxis+0.5*$cplotsize)/mm, ($cplotxaxis-30)/mm);
$ctext->text_center("Physical coordinate (kbp)");
$pdf->finishobjects($cplot,$ctext);
}
# draw chromosome
my $chrob=$page->gfx;
if ($linewidth){
$chrob->linewidth($linewidth);
}
$chrob->fillcolor($chrcolour);
$chrob->strokecolor($chrcolour);
# draw base chromosome.
#print STDERR join(":",$chrx/mm, $basemargin/mm, ($chrwidth)/mm, ($maxheight)/mm), "\n";
#$chrob->rect($chrx/mm, $basemargin/mm, ($chrwidth)/mm, ($maxheight)/mm);
#$chrob->fill;
#$chrob->circle(($chrx+$chrwidth/2)/mm,$basemargin/mm, ($chrwidth/2)/mm);
#$chrob->fill;
#$chrob->circle(($chrx+$chrwidth/2)/mm,($basemargin+$maxheight)/mm, ($chrwidth/2)/mm);
#$chrob->fill;
# DRAW CHROMOSOME OUTLINE
#draw outline
$chrob->move($chrx/mm, $basemargin/mm);
$chrob->line($chrx/mm,($basemargin+$maxheight)/mm);
$chrob->stroke;
$chrob->move(($chrx+$chrwidth)/mm, $basemargin/mm);
$chrob->line(($chrx+$chrwidth)/mm,($basemargin+$maxheight)/mm);
$chrob->stroke;
#print STDERR join(":",($chrx+$chrwidth/2)/mm,($basemargin+$maxheight)/mm, 270, 90, ($chrwidth/2)/mm,($chrwidth/2)/mm,1 ), "\n";
$chrob->arc(($chrx+$chrwidth/2)/mm,($basemargin+$maxheight)/mm, ($chrwidth/2)/mm,($chrwidth/2)/mm, 1, 180,1);
$chrob->stroke;
#print STDERR join(":",($chrx+$chrwidth/2)/mm,$basemargin/mm, ($chrwidth/2)/mm,($chrwidth/2)/mm, 181, 360, 1),"\n";
$chrob->arc(($chrx+$chrwidth/2)/mm,$basemargin/mm, ($chrwidth/2)/mm,($chrwidth/2)/mm,181, 360,1);
$chrob->stroke;
#need to draw molecules after doing the layout? or just redraw with scaling?
#DRAW MOLECULES
#print STDERR "plotting molecules\n";
my @molcolfill=qw/white #CCCCCC/;
my $mc=0;
my $molbins=int(($maxheight/mm)/($binsize*$molfontsize/pt));
#calculate label size needed.
if ($molbins < scalar @molecules) {
$molfontsize=int(($maxheight/mm)/($binsize*(scalar @molecules)/pt));
$molbins=int(($maxheight/mm)/($binsize*$molfontsize/pt));
# print STDERR "$molfontsize ",(scalar @molecules)," ",(pt*$maxheight/mm)," ",($molfontsize*$binsize*(scalar @molecules)),"\n";
if ($molfontsize < $minfont) {
$molfontsize=$minfont;
$molbins=int(($maxheight/mm)/($binsize*$molfontsize/pt));
warn("too many molecules ( ",(scalar @molecules)," in $molbins)");
}
}
#print STDERR (scalar @molecules)," molecules to plot in $molbins slots\n";
# now add in binning code for molecule labels.
my @mollabelbins=();
foreach my $m (sort {$a->{start} <=> $b->{start} } @molecules){
push @{$mollabelbins[int($molbins* ($m->{start}+($m->{length}/2))/$maxmolpos)]}, $m;
$mc++;
my $molob=$page->gfx;
if ($linewidth){
$molob->linewidth($linewidth);
}
$molob->strokecolor($molcolour);
# print STDERR join(":", $m),"\n";
my $endy=($m->{length}/$maxmolpos)*$maxheight;
my $starty=$basemargin+$maxheight-((($m->{start}+$m->{length})/$maxmolpos)* $maxheight); #invert
if ($invert){
$starty=$basemargin+(($m->{start}/$maxmolpos)* $maxheight); #invert
}
$molob->fillcolor($molcolfill[$mc %2]);
$molob->rect( $molx/mm, $starty/mm, ($molwidth)/mm, $endy/mm);
$molob->fill;
$molob->strokecolor($molcolour);
$molob->rect( $molx/mm, $starty/mm, ($molwidth)/mm, $endy/mm);
$molob->stroke;
# draw molecule bands on the correlation and fitted plots. (cpage and dpage)
my $cmolob=$cpage->gfx;
$cmolob->fillcolor($molcolfill[$mc %2]);
$cmolob->rect( ($cplotyaxis+$cplotsize*($m->{start}/$maxmolpos))/mm, $cplotxaxis/mm, $cplotsize * ($m->{length}/$maxmolpos)/mm, $cplotsize/mm);
$cmolob->fill;
my $dmolob=$dpage->gfx;
$dmolob->fillcolor($molcolfill[$mc %2]);
$dmolob->rect( ($cplotyaxis+$cplotsize*($m->{start}/$maxmolpos))/mm, $cplotxaxis/mm, $cplotsize * ($m->{length}/$maxmolpos)/mm, $cplotsize/mm);
$dmolob->fill;
$pdf->finishobjects($molob,$cmolob, $dmolob);
}
#now to draw the labels.
my @labbins=();
my $currentbin=0;
my $lastoccbin=-1;
my $isgroup=0;
my $plotted=0;
# work through the list of labels, plotting in appropriate locations.
while ($currentbin < $molbins){
#print STDERR "current bin $currentbin\n";
if (!defined( @{$mollabelbins[$currentbin]})){
$currentbin++; $isgroup=0;next;
# print STDERR "Bin $currentbin No names - isgroup is $isgroup\n";
}else{
#print STDERR "Bin $currentbin occupied - isgroup is $isgroup\n";
my $plotbin=$lastoccbin+1;
unless ($isgroup){
my $binp=$currentbin;
my $binc=0;
my $bing=0;
while (defined(@{$mollabelbins[$binp]})){
$bing++;
$binc+= scalar @{$mollabelbins[$binp]};
# print STDERR "Grouping Bin $binp - adding ",(scalar @{$mollabelbins[$binp]})," to give $binc\n";
$binp++;
}
# how many locations remain?
my $mremain=(scalar @molecules)-$plotted;
# print STDERR "$mremain molecules left to plot in ",($molbins-$lastoccbin)," slots\n";
# print STDERR "at $currentbin. this group of $binc in $bing slots. Last free is $lastoccbin\n";
while (($molbins-($plotbin) <$mremain) ||
(($plotbin>$lastoccbin) &&
($plotbin>($currentbin-int(($binc-$bing)/2))))) {
$plotbin--;
};
$plotbin++;
$isgroup=1;
}
foreach my $ml (@{$mollabelbins[$currentbin]}){
plotmol($ml, $plotbin); # plots the text and line for the molecule labels.
$lastoccbin=$plotbin;
$plotbin++;
$plotted++;
}
}
$currentbin++;
}
sub plotmol {
my ($m, $plotbin)=@_;
print STDERR "plotting ",$m->{name}," at $plotbin\n";
#need to add text here.
my $moltext=$page->text;
my $linkob=$page->gfx;
# print STDERR "plotting with font size $molfontsize\n";
$moltext->font($moltextfont, $molfontsize/pt);
my $texty=$basemargin+$maxheight - ($maxheight*$plotbin/$molbins);
my $moly=$basemargin+$maxheight-($maxheight*($m->{start}+($m->{length}/2))/$maxmolpos);
if ($invert){
$texty=$basemargin+($maxheight*$plotbin/$molbins);
$moly=($maxheight*($m->{start}+($m->{length}/2))/$maxmolpos)+$basemargin;
}
# print STDERR "text: ",$plotbin/$molbins, "($texty) mol: ", ($m->{start}+($m->{length}/2))/$maxmolpos, "($moly)\n";
#unless (int($molbins*($m->{start}+($m->{length}/2))/$maxmolpos) == $plotbin){
#draw line to molecule midpoint
$linkob->strokecolor(('darkgray','gray')[$plotbin %2]); # molecule label text colour
# alternate between two colours to make it easier to see which molecule is which.
$linkob->move(($mollabelwidth-2)/mm,($texty/mm)+($molfontsize/2*pt));
$linkob->line(($mollabelwidth-10)/mm, $moly/mm);
$linkob->stroke;
$linkob->strokecolor('#CCCCCC'); # molecule label connecting line colour.
$linkob->move(($molx+$molwidth+2)/mm,$moly/mm);
$linkob->line(($mollabelwidth-10)/mm, $moly/mm);
$linkob->stroke;
#}
$moltext->fillcolor(('darkgray','gray')[$plotbin %2]);
$moltext->translate(($mollabelwidth)/mm,$texty/mm);
$moltext->text($m->{name});
$pdf->finishobjects($linkob, $moltext);
}
#print STDERR "plotted molecules\n";
#DRAW MARKERS
my $markerbins=int(($maxheight/mm)/($binsize*$markerfontsize/pt));
# calculate the number of marker label bins (number of rows) to plot.
if ($molposbin) {
$markerbins=int($crowd*$maxmolpos/$molposbin)+1;
$markerfontsize= int(($maxheight/mm)/($binsize*$markerbins/pt));
if ($markerfontsize <$minfont) {
$markerfontsize=$minfont;
$markerbins=int(($maxheight/mm)/($binsize*$markerfontsize/pt));
$molposbin=int($maxmolpos/($crowd*$markerbins))+1;
warn("marker plot resolution too high. Bin rescaled to $molposbin\n");
}
}
print STDERR "Plotting markers into $markerbins bins\n";
# PROCESS MARKERS into bins.
my @seqbins=();
foreach my $mark (@markers) {
my $b=int($markerbins*$mark->{molpos}/$maxmolpos);
$mark->{count}=0;
push @{$seqbins[$b]{$mark->{type}}}, $mark;
${$seqbins[$b]{$mark->{type}}}[0]->{count}++;
#print STDERR "Mark bin $b type $mark->{type} count $mark->{count} bincount ".${$seqbins[$b]{$mark->{type}}}[0]->{count}."\n";
}
#we can now take just the first marker of each type from the bin.
#foreach my $bin (@seqbins){
# foreach my $type (sort keys %$bin){
# for (my $mi=0; $mi<scalar @{$bin->{$type}}; $mi++){
# if ($mi){
# $bin->{$type}[$mi]->{count}=0;
# }else{
# $bin->{$type}[$mi]->{count}=scalar @{$bin->{$type}};
# }
# }
# }
#}
my @markerbins=();
my @mapbins=();
#print STDERR "markerbins $markerbins\n";
foreach my $m (sort {$a->{molpos} <=> $b->{molpos}?$a->{molpos} <=> $b->{molpos}:$a->{name} cmp $b->{name}} @markers) {
$m->{bin}=int($markerbins*$m->{molpos}/$maxmolpos);
if ($m->{count}){
push @{$markerbins[$m->{bin}]}, $m;
} else {
# print STDERR "Skipping marker $m->{name} as count is zero.\n";
}
}
foreach my $m (sort {$a->{avemappos} <=> $b->{avemappos}?$a->{avemappos} <=> $b->{avemappos}:$a->{name} cmp $b->{name}} @markers) {
if ($plotallmap){
$m->{mapbin}=int($markerbins*$m->{avemappos}/$maxchrpos);
}else{
$m->{mapbin}=int($markerbins*binmappos($m->{avemappos})/$maxchrpos);
}
#print STDERR "MAP bin ".join(" : ",$m->{mapbin},$markerbins, $m->{avemappos},$maxchrpos, $m->{name} )."\n";
if ($m->{mapbin} <0){
if ($plotallmap){
push @{$mapbins[0]{"m_".$m->{avemappos}}}, $m;
}else{
push @{$mapbins[0]{"m_0.0"}}, $m;
}
}else{
if ($plotallmap){
push @{$mapbins[$m->{mapbin}]{"m_".$m->{avemappos}}}, $m;
}else{
push @{$mapbins[$m->{mapbin}]{"m_".binmappos($m->{avemappos})}}, $m;
}
}
}
foreach my $l (@markers){
print STDERR "plotting marker $l->{name}\n";
my $linkob=$page->gfx;
my $linktxt=$page->text;
$linkob->strokecolor($links{$l->{type}}{colour});
my $clinkob=$cpage->gfx;
$clinkob->strokecolor($links{$l->{type}}{colour});
## draw cross for marker on correlation plots.
my $cmy=$cplotxaxis+$cplotsize*($l->{avemappos}-$minchrpos)/$cplotyscale;
my $cmx=$cplotyaxis+$cplotsize*$l->{molpos}/$maxmolpos;
$clinkob->move($cmx/mm, ($cmy-2)/mm);
$clinkob->line($cmx/mm, ($cmy+2)/mm);
$clinkob->stroke;
$clinkob->move(($cmx-2)/mm, $cmy/mm);
$clinkob->line(($cmx+2)/mm,$cmy/mm);
$clinkob->stroke;
my $dlinkob=$dpage->gfx;
$dlinkob->strokecolor($links{$l->{type}}{colour});
$dlinkob->move($cmx/mm, ($cmy-2)/mm);
$dlinkob->line($cmx/mm, ($cmy+2)/mm);
$dlinkob->stroke;
$dlinkob->move(($cmx-2)/mm, $cmy/mm);
$dlinkob->line(($cmx+2)/mm,$cmy/mm);
$dlinkob->stroke;
# need to check for a range of positions
my $lcy=0;
my $lmy=$basemargin+$maxheight-($maxheight*$l->{molpos}/$maxmolpos);
if ($invert){
$lmy=($maxheight*$l->{molpos}/$maxmolpos)+$basemargin;
}
# draw marker line
if ($l->{mappos} =~ m/(\d+)-(\d+)/) {
my $s=$1;
my $e=$2;
my $m=$l->{avemappos};
my $lcs=$basemargin+$maxheight-($maxheight*$s/$maxchrpos);
if ($invert) {
$lcs=($maxheight*$s/$maxchrpos)+$basemargin;
}
$lcy=$basemargin+$maxheight-($maxheight*$m/$maxchrpos);
if ($invert){
$lcy=($maxheight*$m/$maxchrpos)+$basemargin;
}
$linkob->move(($chrx)/mm,$lcs/mm);
$linkob->line(($chrx+$chrwidth)/mm, $lcs/mm);
$linkob->stroke;
my $lce=$basemargin+$maxheight-($maxheight*$e/$maxchrpos);
if ($invert){
$lce=($maxheight*$e/$maxchrpos)+$basemargin;
}
$linkob->move(($chrx)/mm,$lce/mm);
$linkob->line(($chrx+$chrwidth)/mm,$lce/mm);
$linkob->stroke;
$linkob->move(($chrx+$chrwidth)/mm, $lcs/mm);
$linkob->line(($chrx+$chrwidth+2)/mm, $lcy/mm);
$linkob->line(($chrx+$chrwidth)/mm, $lce/mm);
$linkob->stroke;
$linkob->move(($chrx)/mm, $lcs/mm);
$linkob->line(($chrx-2)/mm, $lcy/mm);
$linkob->line(($chrx)/mm, $lce/mm);
$linkob->stroke;
}else {
$lcy=$maxheight+$basemargin-($maxheight*$l->{mappos}/$maxchrpos);
if ($invert){
$lcy=($maxheight*$l->{mappos}/$maxchrpos)+$basemargin;
}
$linkob->move(($chrx-2)/mm,$lcy/mm);
$linkob->hline(($chrx+$chrwidth+2)/mm);
$linkob->stroke;
}
$linkob->move(($molx-2)/mm,$lmy/mm);
$linkob->hline(($molx+$molwidth+2)/mm);
$linkob->stroke;
#if ($links{$l->{type}}{line} eq 'dash'){
# $linkob->linedash(2,2);
#} else {
# $linkob->linedash(0);
# }
# draw molecule side horiz line
$linkob->move(($molx-2)/mm,$lmy/mm);
$linkob->line(($chrx+2+$chrwidth)/mm,$lcy/mm);
$linkob->stroke;
# print STDERR "font $links{$l->{type}}{font}\n";
$pdf->finishobjects($linkob,$clinkob,$dlinkob);
}
#$pdf->end();
#label layout.
#Start at top occupied bin.
#1 entity -> plot.
#Is there sufficient space above or below to plot? Plot.
#Plot first n and last m in the n and m spaces above and below. Plot remainder spread in row 2 filling up first.
$currentbin=0;
my $lastoccbin1=-1;
my $lastoccbin2=-1;
while ($currentbin < $markerbins){
# TODO - plot number of markers in a bin and the bin value, not individuals.
#print STDERR "current bin $currentbin\n";
if (!defined( @{$markerbins[$currentbin]})){
$currentbin++; next;
} elsif (scalar @{$markerbins[$currentbin]}==1 ){
my $plotbin=$currentbin;
while ($plotbin<=$lastoccbin1) {$plotbin++;}
plotmarker($markerbins[$currentbin]->[0],$plotbin,0);
$lastoccbin1=$plotbin;
$currentbin++;
}else {
my $toplot=scalar @{$markerbins[$currentbin]};
my $plotbin=$currentbin;
while ($plotbin<=$lastoccbin1) {$plotbin++;}
my $nextoccbin=$plotbin+1;
while (!defined(@{$markerbins[$nextoccbin]}) && $nextoccbin < $markerbins){
$nextoccbin++;
}
if ($nextoccbin-$lastoccbin1 >$toplot) {
for (my $b=0; $b<$toplot; $b++){
my $plotbin=1+$lastoccbin1;
while ($plotbin < $currentbin-$toplot){ $plotbin++;}
plotmarker($markerbins[$currentbin]->[$b], $plotbin,0);
$lastoccbin1=$plotbin;
}
}else{
my $upset=$currentbin-($lastoccbin1+1);
my $upset2=$toplot-$upset;
my $start2=$currentbin-$upset2;
while ($start2<=$lastoccbin2) {
$start2++;
}
for (my $p=0; $p<$upset; $p++){
my $plotbin=$currentbin+$p-$upset;
plotmarker($markerbins[$currentbin]->[$p],$plotbin,0);
$lastoccbin1=$plotbin;
}
$lastoccbin1=$currentbin;
for (my $s=0; $s<$toplot-$upset;$s++){
my $plotbin=$start2+$s;
plotmarker($markerbins[$currentbin]->[$s+$upset],$plotbin, 1);
$lastoccbin2=$plotbin;
}
}
$currentbin++;
}
}
## Plotting genetic map markers
# plot unanchored
foreach my $m (@unanchored){plotunmap($m);}
#plot anchored
$currentbin=0;
$lastoccbin1=-1;
$lastoccbin2=-1;
while ($currentbin <= $markerbins){
my @keys=();
#print STDERR "current bin $currentbin\n";
if (!defined( %{$mapbins[$currentbin]})){
$currentbin++; next;
} else {
@keys=sort {substr($a,2) <=> substr($b,2)} keys %{$mapbins[$currentbin]};
}
if (scalar @keys==1 ){
my $plotbin=$currentbin;
while ($plotbin<=$lastoccbin1) {$plotbin++;}
plotmap($keys[0], scalar @{$mapbins[$currentbin]{$keys[0]}}, $plotbin,0);
$lastoccbin1=$plotbin;
$currentbin++;
}else {
my $toplot=scalar @keys;
my $plotbin=$currentbin;
while ($plotbin<=$lastoccbin1) {$plotbin++;}
my $nextoccbin=$plotbin+1;
while (!defined(%{$mapbins[$nextoccbin]}) && $nextoccbin < $markerbins){
$nextoccbin++;
}
if ($nextoccbin-$lastoccbin1 >$toplot) {
for (my $b=0; $b<$toplot; $b++){
my $plotbin=1+$lastoccbin1;
while ($plotbin < $currentbin-$toplot){ $plotbin++;}
plotmap($keys[$b], scalar @{$mapbins[$currentbin]{$keys[$b]}}, $plotbin,0);
$lastoccbin1=$plotbin;
}
}else{
my $upset=$currentbin-($lastoccbin1+1);
my $upset2=$toplot-$upset;
my $start2=$currentbin-$upset2;
while ($start2<=$lastoccbin2) {
$start2++;
}
for (my $p=0; $p<$upset; $p++){
my $plotbin=$currentbin+$p-$upset;
plotmap($keys[$p],scalar @{$mapbins[$currentbin]{$keys[$p]}},$plotbin,0);
$lastoccbin1=$plotbin;
}
$lastoccbin1=$currentbin;
for (my $s=0; $s<$toplot-$upset;$s++){
my $plotbin=$start2+$s;
plotmap($keys[$s+$upset], scalar @{$mapbins[$currentbin]{$keys[$s+$upset]}},$plotbin, 1);
$lastoccbin2=$plotbin;
}
}
$currentbin++;
}
}
my $titletxt=$page->text;
$titletxt->font($titletextfont,$titlefontsize/pt);
$titletxt->translate(($molx+$molwidth/2)/mm, ($basemargin+$maxheight+5)/mm);
$titletxt->fillcolor($titlecolour);
my $fulltitle=$plottitle;
unless ($nosize){
$fulltitle .= " ($maxmolpos bp)";
}
$titletxt->text_center($fulltitle);
$pdf->finishobjects($titletxt);
# need to pull back all markers in rearranged molecule and plot
my @repmarkers=sort {int($a->{molpos} <=>$b->{molpos})} $assembly->getMarkers($mapname);
print STDERR "Retrieved ".(scalar @repmarkers)." markers\n";
foreach my $m (@repmarkers){
my $elinkob=$dpage->gfx;
$elinkob->strokecolor($links{$m->{type}}{colour});
my $cmy=$cplotxaxis+$cplotsize*($m->{mappos}-$minchrpos)/$cplotyscale;
my $cmx=$cplotyaxis+$cplotsize*$m->{molpos}/$maxmolpos;
$elinkob->circle($cmx/mm, $cmy/mm, 1/mm);
$elinkob->stroke;
$pdf->finishobjects($elinkob);
}
my $oldline=$dpage->gfx;
$oldline->strokecolor('red');
my $sp=$repmarkers[0]->{molpos};
my $ep=$repmarkers[$#repmarkers]->{molpos};
#print STDERR "plotting original curve between $sp and $ep\n";
my $step=int(($ep-$sp)/200);# ca. 200 steps to draw the line.
my $lastx=0;
my $lastyold=0;
#print "parameters are $report->{assembly}{final}{a0} $report->{assembly}{final}{a1} $report->{assembly}{final}{a2} $report->{assembly}{final}{a3}\n";
for (my $pos=$sp; $pos<$ep; $pos += $step){
my $xc=$cplotyaxis+$cplotsize*$pos/$maxmolpos;
my $yo=gety($pos, $report,0);
my $yco=$cplotxaxis+$cplotsize*($yo-$minchrpos)/$cplotyscale;
#print "plotting coordinates $xc, $yco\n";
if ($lastx){
$oldline->move($lastx/mm,$lastyold/mm);
$oldline->line($xc/mm,$yco/mm);
$oldline->stroke;
}else{
$oldline->move($xc/mm,$yco/mm);
}
$lastx=$xc;
$lastyold=$yco;
}
my $newline=$dpage->gfx;
$newline->strokecolor('blue');
$lastx=0;
my $lastynew=0;
for (my $pos=$sp; $pos<$ep; $pos += $step){
my $xc=$cplotyaxis+$cplotsize*$pos/$maxmolpos;
my $yn=gety($pos, $report,1);
my $ycn=$cplotxaxis+$cplotsize*($yn-$minchrpos)/$cplotyscale;
if ($lastx){