-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDensityMap.pl
executable file
·1110 lines (912 loc) · 44.6 KB
/
DensityMap.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 strict;
use diagnostics;
use warnings;
use Getopt::Long;
#use Term::ANSIColor;
#use Data::Dumper;
use GD::SVG;
use POSIX;
use Cwd 'abs_path';
use File::Basename;
#> Setting Parameters
##> Define outputs colors
#print STDOUT color 'blue';
#print STDERR color 'red';
##> Define options
my %config;
GetOptions (\%config,
'input=s',
'region_file=s',
'type_to_draw=s',
'output_img_name=s',
'rounding_method=s',
'show_scale=i',
'str_width=i',
'str_space=i',
'space_chr=i',
'scale_factor=i',
'auto_scale_factor=i',
'background=s',
'transparency=s',
'lmargin=i',
'rmargin=i',
'tmargin=i',
'bmargin=i',
'label_strand_rotation=i',
'title=s',
'force',
'win_size=i',
'verbose',
'debug',
'colour_scale=i',
'gc=i',
'ft_family=s',
'ft_size=i');
##> Print USAGE if --help
if ($config{help}) {printUsage(1);}
##> Check if gff file exist, if no mandatory parameter are missing
if (!exists $config{input} or
!exists $config{output_img_name} or
!exists $config{type_to_draw}) {printError ("\n!!!!gff, output_img_name, type_to_draw options are MANDATORY !!!!! \n\n\n", 0); printUsage(1);}
#if (! -e $config{input}) {printError ("gff $config{input} not exist ! \n"); printUsage(1);}
##> Setting Global Variables
my $paternType = "(";
my $scaleAddWidth = 100;
my $numMaxTicks;
my $numTicks;
my $chr_length;
my $chr_length_reel;
my $scale_factor;
my $strand_width;
my $strand_space;
my $label_strand_rotation;
my $picHeight;
my $picWidth;
my $colour_scale;
my $count = 0;
my $countGff = -1;
my $win_size;
my $rounding_method;
my $gc_cs;
my $font;
my $fts = 16;
my $space_chr;
my %color;
my %gffTypes;
my %margin;
my %order;
$order{'-'} = "-";
$order{'+'} = "+";
$order{'both'} = "-;+";
$order{'fused'} = "-+";
$order{'all'} = "-;-+;+";
my %rand;
my %offset;
$offset{'x'} = 0;
$offset{'y'} = 0;
$offset{'x1'} = 0;
$offset{'y1'} = 0;
$offset{'x2'} = 0;
$offset{'y2'} = 0;
$| = 1;
if (!exists $config{show_scale}) {$numMaxTicks = 50;}
else {$numMaxTicks = $config{show_scale};}
if (!exists $config{str_width}) {$strand_width = 50;}
else {$strand_width = $config{str_width};}
if (!exists $config{str_space}) {$strand_space = 50;}
else {$strand_space = $config{str_space};}
if (!exists $config{space_chr}) {$space_chr = 50;}
else {$space_chr = $config{space_chr};}
if ($config{auto_scale_factor}) {$scale_factor = 1;}
elsif (!exists $config{scale_factor}) {$scale_factor = 1000;}
else {$scale_factor = $config{scale_factor};}
if (!exists $config{lmargin}) {$margin{l} = 50;}
else {$margin{l} = $config{lmargin};}
if (!exists $config{rmargin}) {$margin{r} = 50;}
else {$margin{r} = $config{rmargin};}
if (!exists $config{tmargin}) {$margin{t} = 50;}
else {$margin{t} = $config{tmargin};}
if (!exists $config{bmargin}) {$margin{b} = 50;}
else {$margin{b} = $config{bmargin};}
if (!exists $config{label_strand_rotation}) {$label_strand_rotation = 0;}
else {$label_strand_rotation = $config{label_strand_rotation};}
if (!exists $config{colour_scale}) {$colour_scale = 7;}
else {$colour_scale = $config{colour_scale};}
if (!exists $config{gc}) {$gc_cs = 7;}
else {$gc_cs = $config{gc};}
if (!exists $config{win_size}) {$win_size = 1;}
else {$win_size = $config{win_size};}
if (!exists $config{rounding_method}) {$rounding_method = "floor";}
else {$rounding_method = $config{rounding_method};}
if (!exists $config{ft_size}) {$fts = 16;}
else {$fts = $config{ft_size};}
print "gffs : $config{input}\n" if $config{verbose};
print "output_img_name : $config{output_img_name}\n" if $config{verbose};
##> Setting parameters
# -1. Load region if defined
my %region;
if ($config{region_file}) {
open(REGION, "<$config{region_file}") or die "Can not open $config{region_file} ! ";
while (<REGION>) {
chomp;
my @bed = split("\t");
$region{$bed[0]}{length} = $bed[2]-$bed[1];
$region{$bed[0]}{start} = $bed[1];
$region{$bed[0]}{end} = $bed[2];
}
close REGION;
}
# 1. Get Image size
## 1.1 Height (+Check GFF validity)
print "Searching Max Sequence Length ... \n" if $config{debug};
my $numOfGff = 0;
my %listChr;
my $maxSequenceLength = 0;
open(GFF, "<$config{input}") or printError("Could not open $config{input}\n", 1);
my $initHeadSeq;
my $initSeq;
my $switchInitFasta = 0;
while (<GFF>) {
chomp;
if (/##sequence-region\s+(\S+)\s+\d+\s+(\d+)/) {
$numOfGff++;
$initHeadSeq = $1;
$listChr{$initHeadSeq}{length} = $2;
}
if (/>(.+)/) {
$initHeadSeq = $1;
$switchInitFasta = 1;
}
elsif ($switchInitFasta) {
$listChr{$initHeadSeq}{seq} .= $_;
}
}
close GFF;
if ($config{region_file}) {
foreach my $k (keys %region){
$maxSequenceLength = ($region{$k}{length} > $maxSequenceLength) ? $region{$k}{length} : $maxSequenceLength;
}
}
else {
foreach my $k (keys %listChr){
$maxSequenceLength = ($listChr{$k}{length} > $maxSequenceLength) ? $listChr{$k}{length} : $maxSequenceLength;
}
}
#foreach my $file (split(/;/, $config{input})){
# print "\tLooking at $file \n" if $config{debug};
#
# open(GFF, "<$file") or die "Can not open $file ! \n";
# $numOfGff++;
#
# my $first_line = <GFF>;
# printError ("Not GFF3 format ! (1)\n", 1) if $first_line !~ /##gff-version 3/;
#
# $first_line = <GFF>;
# printError ("Not GFF3 format ! (2)\n", 1) if $first_line !~ /##sequence-region\s+[^\s]+\s+\d+\s+(\d+)/;
#
# $maxSequenceLength = ($1 > $maxSequenceLength) ? $1 : $maxSequenceLength;
# close GFF;
#}
if ($config{title}) {$margin{'t'} += 40};
if ($config{auto_scale_factor}) {
while (1) {
$picHeight = $margin{'t'}
+ $margin{'b'}
+ (floor(($maxSequenceLength/$scale_factor)*$win_size));
last if ($picHeight < $config{auto_scale_factor});
$scale_factor *= 10;
}
print "Selected Scale Factor = $scale_factor\n";
}
else{
$picHeight = $margin{'t'}
+ $margin{'b'}
+ (floor(($maxSequenceLength/$scale_factor)*$win_size));
}
## 1.2 Width (+Check type option)
my $numOfStrand = 0;
my @type_array;
foreach (split(/;/, $config{'type_to_draw'})){
if($_=~ /([^=]+)=([^=]+)/){
push @type_array, $1;
if ($2 eq "-" or $2 eq "+" or $2 eq "fused") {$numOfStrand++;}
elsif($2 eq "both") {$numOfStrand += 2;}
elsif($2 eq "all") {$numOfStrand += 3;}
else {printError("Type option : INVALID STRAND FORMAT, check help please.\n", 1);}
}
else {printError("Type option : INVALID FORMAT, check help please.\n", 1);}
}
$margin{'l'} = $margin{'l'} + $scaleAddWidth if ($config{show_scale});
$picWidth = $margin{'l'}
+ $margin{'r'}
+ (($numOfGff * $numOfStrand ) * $strand_width)
#+ (($numOfGff * $numOfStrand - 1) * $strand_space);
+ (($numOfGff * ($numOfStrand - 1)) * $strand_space)
+ (($numOfGff - 1) * $space_chr);
if ($config{gc}){
$picWidth += ($numOfGff * $strand_width)
+ ($numOfGff * $strand_space);
}
## 1.3 Ask if image size is OK
if (!$config{force}) {
while (1) {
print "Is the picture size will be ok ? ($picWidth px x $picHeight px) [y/n] : ";
chomp (my $response = <STDIN>);
if ($response eq "n") {exit;}
elsif ($response eq "y") {last;}
}
}
# 2 Create picture
print "Create Picture ...\n" if $config{'verbose'};
my $image = GD::SVG::Image->new($picWidth, $picHeight);
# 3 Loading colors from colours.txt
print "Load colours ...\n" if $config{'verbose'};
open(COLOR, "<".dirname(abs_path($0))."/colours.txt") or die "Can not open colours.txt";
while (<COLOR>) {
next if /^#/;
/([\d\w]+);(\d+);(\d+);(\d+)/;
$color{lc($1)} = $image->colorAllocate($2,$3,$4);
}
close COLOR;
# 4 Draw Background
if ($config{'background'}) {
print "Draw background ...\n" if $config{'verbose'};
$image->filledRectangle(0, 0, $picWidth, $picHeight, $color{$config{'background'}});
}
# 5 Drawing Title and Scale
if ($config{title}){
if ($config{show_scale}) {
$image->string( gdLargeFont,
$scaleAddWidth + ($picWidth - $scaleAddWidth) / 2 - ((gdLargeFont->width * length $config{title})/2),
20 - ((gdLargeFont->height) / 2),
$config{title},
$color{'black'});
}
else {
$image->string( gdLargeFont,
$picWidth / 2 - ((gdLargeFont->width * length $config{title})/2),
20 - ((gdLargeFont->height) / 2),
$config{title},
$color{'black'});
}
}
if ($config{show_scale}) {
print "Drawing Scale ...\n" if $config{verbose};
my $scale_size = floor($maxSequenceLength/$scale_factor);
print "Scale_size : $scale_size\n" if $config{debug};
drawScale(\$image, $scale_size, $maxSequenceLength, $scaleAddWidth, $scale_factor, $numMaxTicks, $margin{'t'}, $win_size);
}
# 6 Get Type/Strand
foreach (split(/;/, $config{'type_to_draw'})){
$_=~ /([^=]+)=([^=]+)=?(\d*)/;
print "type = $1\tstrand = $2\n" if $config{debug};
if ($3) {$gffTypes{$1}{colour} = $3;}
else {$gffTypes{$1}{colour} = $colour_scale;}
$gffTypes{$1}{strand} = $2;
$gffTypes{$1}{'-'} = [];
$gffTypes{$1}{'+'} = [];
$gffTypes{$1}{'-+'} = [];
$paternType .= $1.'|';
}
#$paternType =~ s/\|$//g;
$paternType .= 'centromere)';
print "Patern Regexp = $paternType\n" if $config{debug};
# 7 Foreach file draw strand(s)
open(GFF, "<$config{input}") or printError("Could not open $config{input}\n", 1);
my $seqName;
my $switchFirstSetLoaded = 0;
my %centromere;
my $csv = $config{output_img_name};
$csv =~ s/.svg/.csv/g;
open(CSV, ">$csv") or die "Can not open $csv ! ";
print CSV "sequence\tfeature\tstart\tend\tdensity\n";
while (<GFF>) {
if (/##sequence-region\s+(\S+)\s+\d+\s+(\d+)/) {
if ($switchFirstSetLoaded){
processData();
$countGff++;
}
$switchFirstSetLoaded = 1;
$seqName = $1;
$chr_length_reel = $2;
$chr_length = floor($chr_length_reel/$scale_factor);
# 7.1 Load GFF
print "Loading $seqName ...\n" if $config{verbose};
foreach (keys(%gffTypes)){
$gffTypes{$_}{'-'} = [];
$gffTypes{$_}{'+'} = [];
$gffTypes{$_}{'-+'} = [];
}
%centromere = ();
}
elsif ($seqName && /^$seqName/) {
my @line = split(/\t/);
next if $line[2] !~ /$paternType/;
if ($line[2] eq "centromere") {
$centromere{start} = $line[3];
$centromere{end} = $line[4];
next;
}
my $ref_tabM = $gffTypes{$line[2]}{'-'};
my $ref_tabP = $gffTypes{$line[2]}{'+'};
my $ref_tabMP = $gffTypes{$line[2]}{'-+'};
if ($line[6] eq "-") {push(@{$ref_tabM}, [$line[3], $line[4]]);}
elsif ($line[6] eq "+") {push(@{$ref_tabP}, [$line[3], $line[4]]);}
push(@{$ref_tabMP}, [$line[3], $line[4]]);
}
elsif(/##FASTA/) {last;}
}
close GFF;
processData();
close CSV;
# 8 Applying rotation to labels and Saving picture
open(IMG, ">$config{output_img_name}") or die "Can not open $config{output_img_name} ! ";
binmode IMG;
my @text = split("\t", $image->svg);
#if (!$config{transparency}) {
my $switchRotate = 0;
foreach (@text){
if ($config{label_strand_rotation} and /<g id="rotate_\d+">/) {$switchRotate++;}
elsif ($config{label_strand_rotation} and /<\/g>/ and $switchRotate) {$switchRotate--;}
if ($switchRotate) {s/x="([\d\.]+)" y="([\d\.]+)"/x="$1" y="$2" transform="rotate($label_strand_rotation, $1, $2)"/;}
if ($config{ft_family}) {s/font="Helvetica"/font-family="$config{ft_family}"/;}
if ($fts != 16) {s/font-size="16"/font-size="$fts"/;}
#s/stroke-opacity: 1.0; stroke-width: 1/stroke: none/g;
#s/stroke-opacity: 1.0;/stroke: none;/g;
s/stroke-opacity: 1.0;?//g;
s/stroke-width: 1;?//g;
s/stroke: rgb\(\d+,\d+,\d+\);?//g;
s/ +/ /g;
s/style=" /style="/g;
s/;? " width/" width/g;
print IMG $_;
}
#}
#WAS USED FOR ADDING TRANSPARENCY ON MERGED TRACKS
#else {
# my $switch = 0;
# my $switchRotate = 0;
#
# foreach (@text){
# #s/stroke\-width\: 1/stroke\-width\: 0/g;
# s/stroke-opacity: 1.0; stroke-width: 1/stroke: none/g;
# if (/<g id="\-\+_\d+">/) {$switch++;}
# elsif (/<\/g>/ and $switch) {$switch--;}
#
# if (/<g id="rotate_\d+">/) {$switchRotate++;}
# elsif (/<\/g>/ and $switchRotate) {$switchRotate--;}
#
# if ($switch) {
# s/fill-opacity: 1.0/fill-opacity: $config{transparency}/g;
# s/stroke-opacity: 1.0; stroke-width: 1/stroke: none/g;
# }
# if ($switchRotate) {s/x="(\d+)" y="(\d+)"/x="$1" y="$2" transform="rotate(-45, $1, $2)"/;}
# print IMG $_;
# }
#}
close IMG;
print "Image saved ! \n";
###########################################################################
################################ Fonctions ################################
###########################################################################
sub processData{
# 7.2 Sorting intervals
foreach (keys(%gffTypes)){
my $ref_tabM = $gffTypes{$_}{'-'};
my $ref_tabP = $gffTypes{$_}{'+'};
my $ref_tabMP = $gffTypes{$_}{'-+'};
@{$ref_tabM} = sort {$a->[0] <=> $b->[0]} @{$ref_tabM};
@{$ref_tabP} = sort {$a->[0] <=> $b->[0]} @{$ref_tabP};
@{$ref_tabMP} = sort {$a->[0] <=> $b->[0]} @{$ref_tabMP};
}
# 7.3 Reducing intervals
foreach (keys(%gffTypes)){
$gffTypes{$_}{'-'} = removeIntervalRedundancy(@{$gffTypes{$_}{'-'}});
$gffTypes{$_}{'+'} = removeIntervalRedundancy(@{$gffTypes{$_}{'+'}});
$gffTypes{$_}{'-+'} = removeIntervalRedundancy(@{$gffTypes{$_}{'-+'}});
}
# 7.4 Set Offset
foreach (@type_array){
if ($gffTypes{$_}{'strand'} eq "-") {
$offset{$_}{'-'}{'x'} = $margin{'l'} + ($count * $strand_width) + ($count * $strand_space) - ($countGff * $strand_space) + ($countGff * $space_chr);
$offset{$_}{'-'}{'y'} = $margin{'t'};
$count++;
print "x = $offset{$_}{'-'}{'x'}\ny = $offset{$_}{'-'}{'y'}\n" if $config{debug};
}
elsif ($gffTypes{$_}{'strand'} eq "+") {
$offset{$_}{'+'}{'x'} = $margin{'l'} + ($count * $strand_width) + ($count * $strand_space) - ($countGff * $strand_space) + ($countGff * $space_chr);
$offset{$_}{'+'}{'y'} = $margin{'t'};
$count++;
print "+ = $offset{$_}{'+'}{'x'}\n+ = $offset{$_}{'+'}{'y'}\n" if $config{debug};
}
elsif ($gffTypes{$_}{'strand'} eq "both") {
$offset{$_}{'-'}{'x'} = $margin{'l'} + ($count * $strand_width) + ($count * $strand_space) - ($countGff * $strand_space) + ($countGff * $space_chr);
$offset{$_}{'-'}{'y'} = $margin{'t'};
$count++;
$offset{$_}{'+'}{'x'} = $margin{'l'} + ($count * $strand_width) + ($count * $strand_space) - ($countGff * $strand_space) + ($countGff * $space_chr);
$offset{$_}{'+'}{'y'} = $margin{'t'};
$count++;
print "+ = $offset{$_}{'-'}{'x'}\n+ = $offset{$_}{'-'}{'y'}\n- = $offset{$_}{'+'}{'x'}\n- = $offset{$_}{'+'}{'y'}\n" if $config{debug};
}
elsif ($gffTypes{$_}{'strand'} eq "fused") {
$offset{$_}{'-+'}{'x'} = $margin{'l'} + ($count * $strand_width) + ($count * $strand_space) - ($countGff * $strand_space) + ($countGff * $space_chr);
$offset{$_}{'-+'}{'y'} = $margin{'t'};
$count++;
print "f = $offset{$_}{'-+'}{'x'}\nf = $offset{$_}{'-+'}{'y'}\n" if $config{debug};
}
elsif ($gffTypes{$_}{'strand'} eq "all") {
$offset{$_}{'-'}{'x'} = $margin{'l'} + ($count * $strand_width) + ($count * $strand_space) - ($countGff * $strand_space) + ($countGff * $space_chr);
$offset{$_}{'-'}{'y'} = $margin{'t'};
$count++;
$offset{$_}{'-+'}{'x'} = $margin{'l'} + ($count * $strand_width) + ($count * $strand_space) - ($countGff * $strand_space) + ($countGff * $space_chr);
$offset{$_}{'-+'}{'y'} = $margin{'t'};
$count++;
$offset{$_}{'+'}{'x'} = $margin{'l'} + ($count * $strand_width) + ($count * $strand_space) - ($countGff * $strand_space) + ($countGff * $space_chr);
$offset{$_}{'+'}{'y'} = $margin{'t'};
$count++;
print "- = $offset{$_}{'-'}{'x'}\n- = $offset{$_}{'-'}{'y'}\nf = $offset{$_}{'-+'}{'x'}\nf = $offset{$_}{'-+'}{'y'}\n+ = $offset{$_}{'+'}{'x'}\n+ = $offset{$_}{'+'}{'y'}\n" if $config{debug};
}
}
if ($config{gc}){
$offset{'gc'}{'x'} = $margin{'l'} + ($count * $strand_width) + ($count * $strand_space) - ($countGff * $strand_space) + ($countGff * $space_chr);
$offset{'gc'}{'y'} = $margin{'t'};
$count++;
}
foreach my $typeToDraw (@type_array){
print "typeToDraw = $typeToDraw\n" if $config{'debug'};
foreach my $strand (split(";", $order{$gffTypes{$typeToDraw}{'strand'}})){
print "strand = $strand\n" if $config{'debug'};
my $ref_tab = $gffTypes{$typeToDraw}{$strand};
my $cs = $gffTypes{$typeToDraw}{colour};
drawPixels(\$image, \%rand, $cs, $seqName, $chr_length, $scale_factor, $typeToDraw, $strand, $strand, \%centromere, $ref_tab, $win_size);
}
}
if ($config{gc}){
printError("Fasta sequence is not in the gff file ! ", 1) if (!$listChr{$seqName}{seq});
drawPixelsGC(\$image, \%rand, $gc_cs, $seqName, $chr_length, $scale_factor, $win_size, \$listChr{$seqName}{seq});
}
}
###########################################################################
sub removeIntervalRedundancy{
# The purpose of this function is to remove or merging the feature of the gff
# Input : sorted array of start - end array
# Output : sorted array of start - end array without crossing intervals
#my ($ref_intervalsFused, @gff) = @_;
my (@gff) = @_;
my @reduced_gff;
my $bool_firstInterval = 1;
print "Start removeIntervalRedundancy ... \n" if $config{'verbose'};
print "\tStarting gff size = ".@gff."\n" if $config{'verbose'};
foreach my $annot (@gff) {
# The intervals of the gff have been sorted by 'start' before using this function
# so only three case are possebles :
# #1 Previous and current intervals crossing
# #2 Current interval included in previous interval
# #3 Current interval not inclued in previous interval
#######################################################
################### Possibles Cases ###################
####################################################### x + <- Current start/end interval
# Start End # <-<-<- Previous interval
#######################################################
#1 | x ] + # Intervales chevauchants
#2 | x + ] # Intervales inclu dans le précédent
#3 | | x + # Intervales non-chevauchants
#######################################################
print "\t---> Current interval = $annot->[0], $annot->[1]\n" if $config{insaneDebugMode};
#Load first interval
if ($bool_firstInterval) {
print "\nLoad first interval\n" if $config{'insaneDebugMode'};
push(@reduced_gff, [$annot->[0], $annot->[1]]);
$bool_firstInterval--;
print "\tannot = $annot\n" if $config{insaneDebugMode};
print "\t\$annot->[0], \$annot->[1] = $annot->[0], $annot->[1]\n" if $config{insaneDebugMode};
print "\t\@reduced_gff size = ".@reduced_gff."\n\n" if $config{insaneDebugMode};
next;
}
if ($annot->[0] <= $reduced_gff[$#reduced_gff]->[1] and #1
$annot->[1] > $reduced_gff[$#reduced_gff]->[1]) {
# Replace previous end
$reduced_gff[$#reduced_gff]->[1] = $annot->[1];
}
elsif ($annot->[0] <= $reduced_gff[$#reduced_gff]->[1] and #2
$annot->[1] <= $reduced_gff[$#reduced_gff]->[1]) {
# Go to next interval
next;
}
else { #3
# Insert interval in reduced gff
push(@reduced_gff, [$annot->[0], $annot->[1]]);
}
} #End foreach (@gff)
print "\tFinal gff size = ".@reduced_gff."\n" if $config{'verbose'};
print "Finished\n" if $config{'verbose'};
return \@reduced_gff;
}
###########################################################################
sub drawScale{
# Drawing the scale on the left side of graph
# Input :
# - $ref_img -> reference of the image
# - $chr_size -> chromosome/sequence size divided by scale_factor
# - $chr_size_reel -> chromosome/sequence size
# - $widthScale -> Width of the scale
# - $basesPerPixel -> Number of bases per pixels (scale_factor)
# - $maxTicks -> Number Max of ticks
# - $marginTop -> size of top margin
# Output : none
my ($ref_img, $chr_size, $chr_size_reel, $widthScale, $basesPerPixel, $maxTicks, $marginTop, $win_size) = @_;
my $basesPerTicks = 10;
# Search the number of tick to use
print "Start searching num ticks\n" if $config{debug};
while (1) {
$numTicks = floor($chr_size/$basesPerTicks);
print "basesPerTicks = $basesPerTicks \t numTicks = $numTicks\n" if $config{debug};
last if ($numTicks <= $maxTicks);
$basesPerTicks*=10;
}
print "Found num ticks\n" if $config{debug};
# Define the 10^x bases to use as unit
my $power = floor(log10($basesPerTicks*$basesPerPixel));
# Add label unit
my $string = "(x10e$power bases)";
$$ref_img->string(gdSmallFont,
$widthScale - 15 - (gdSmallFont->width * length $string),
$marginTop - 5,
$string,
$color{'black'});
#Add ratio pixel base label (2 lines)
$string = "(1 win = ";
$$ref_img->string(gdSmallFont,
$widthScale - 15 - (gdSmallFont->width * length "$basesPerPixel bases)"),
$marginTop - 5 + gdSmallFont->height,
$string,
$color{'black'});
$string = "$basesPerPixel bases)";
$$ref_img->string(gdSmallFont,
$widthScale - 15 - (gdSmallFont->width * length $string),
$marginTop - 5 + (gdSmallFont->height * 2),
$string,
$color{'black'});
# Draw scale
print "\$\$ref_img->filledRectangle($widthScale - 10, $marginTop, $widthScale - 8, $marginTop + $chr_size, \$color{'black'})\;\n" if $config{debug};
$$ref_img->filledRectangle($widthScale - 10,
$marginTop,
$widthScale - 8,
$marginTop + $chr_size * $win_size,
$color{'black'});
# Draw scale last tick
$string = sprintf("%.2f", $chr_size_reel/(10**$power));
$$ref_img->string(gdLargeFont,
#$widthScale - 15 - (gdLargeFont->width * length $string),
$widthScale - 15,
#$marginTop + $chr_size - 8,
$marginTop + $chr_size * $win_size,
$string,
$color{'black'});
print "\$\$ref_img->filledRectangle($widthScale - 10, $marginTop + $chr_size - 1, $widthScale, $marginTop + $chr_size, \$color{'black'})\;\n" if $config{debug};
$$ref_img->filledRectangle($widthScale - 10,
$marginTop + $chr_size * $win_size - 1,
$widthScale,
$marginTop + $chr_size * $win_size,
$color{'black'});
#Draw scale other ticks
for (my $currentTick = 0 ; $currentTick <= $numTicks ; $currentTick++) {
$string = sprintf("%.2f", ((($basesPerTicks * $currentTick) * $basesPerPixel) /10**$power));
$string = 0 if ($currentTick == 0);
$$ref_img->string(gdLargeFont,
$widthScale - 15 -(gdLargeFont->width * length $string),
$marginTop + ($basesPerTicks * $currentTick) * $win_size - 8,
$string,
$color{'black'});
print "\$\$ref_img->filledRectangle($widthScale - 10, $marginTop + ($basesPerTicks * $currentTick), $widthScale, $marginTop + ($basesPerTicks * $currentTick) + 1, \$color{'black'})\;\n" if $config{debug};
$$ref_img->filledRectangle($widthScale - 10,
$marginTop + ($basesPerTicks * $currentTick) * $win_size,
$widthScale,
$marginTop + ($basesPerTicks * $currentTick) * $win_size + 1,
$color{'black'});
}
}
###########################################################################
sub drawPixels{
# Draw each pixel of strand
# Input :
# - $ref_img -> ref of the image
# - $ref_rand -> ref on the hash of random numbers
# - $ref_colour_scale -> colour_scale to use for colors
# - $chr_size -> Chromosome/Sequence size
# - $scaleFactor -> Scale_factor
# - $type -> current to draw (label)
# - $strand -> current strand in process (label)
# - $strandColor -> color to use with strand
# - $ref_gff -> ref of the cureent strand gff
# Ouput : none
print "Start Drawing pixels ...\n" if $config{'verbose'};
my ($ref_img, $ref_rand, $cs, $seqName, $chr_size, $scaleFactor, $type, $strand, $strandColor, $ref_centromere, $ref_gff, $win_size) = @_;
my @gff = @{$ref_gff};
my %centro = %{$ref_centromere};
my $randNum;
my %intervals;
my @previousBases = (0);
# Search a unique random number
while (1) {
$randNum = int rand(1000);
redo if $$ref_rand{$randNum}++;
last;
}
print "\tchr_size = $chr_size\n" if $config{'debug'};
print "\tscaleFactor = $scaleFactor\n" if $config{'debug'};
print "\ttype = $type\n" if $config{'debug'};
print "\tstrand = $strand\n" if $config{'debug'};
# Open chromosome/sequence group
$$ref_img->startGroup("${strand}_${randNum}");
# For each pixel of the chromosome/sequence
my $posPic = 0;
for (my $pos = 0 ; $pos <= $chr_size ; $pos++) {
next if ($config{region_file} && (($pos*$scaleFactor) < $region{$seqName}{start} || ($pos*$scaleFactor+$scaleFactor) > $region{$seqName}{end}));
$posPic++;
# Get number of base covered by the previous on pixel crosssing interval
my $basesCoverred = shift @previousBases;
$basesCoverred = 0 if (!defined $basesCoverred); # if @previousBases is empty
print "pos = $pos\n" if $config{'debug'};
print "basesCoverred Tab = -".join(" ", @previousBases)."-\n" if $config{'debug'};
print "basesCoverred = $basesCoverred (previous shift)\n" if $config{'debug'};
print "Start while\n" if $config{'debug'};
print "\$gff[0]->[0] = $gff[0]->[0]\n" if $config{'debug'};
#printError( "\$gff[0]->[0] undef ! \n") if(!defined($gff[0]->[0]));
# while the end of gff is not reached and the next start is in the current pixel
while (defined($gff[0]->[0]) and $gff[0]->[0] < ($pos * $scaleFactor)) {
my $ref_interval= shift(@gff);
last if (!defined $ref_interval->[0]); # = defined($gff[0]->[0]) avoid warnings
# get clearly start and end
$intervals{'start_reel'} = $ref_interval->[0];
$intervals{'end_reel'} = $ref_interval->[1];
print "\tstart_reel = $intervals{'start_reel'}\n" if $config{'debug'};
print "\tend_reel = $intervals{'end_reel'}\n" if $config{'debug'};
print "\tcheck position\n" if $config{'debug'};
# feature end is on current pixel
if ($intervals{'end_reel'} < ($pos * $scaleFactor) ){
print "\tCase 1 : \$basesCoverred += $intervals{'end_reel'} - $intervals{'start_reel'}\n" if $config{'debug'};
print "\tCase 1 : \$basesCoverred += ".($intervals{'end_reel'} - $intervals{'start_reel'})."\n" if $config{'debug'};
$basesCoverred += $intervals{'end_reel'} - $intervals{'start_reel'}; # increment the base counting
}
# feature end is on next pixels
else{
# determine on how much pixel the interval is crossing
my $numPixelImpliy = floor(($intervals{'end_reel'} - $intervals{'start_reel'} - (($pos * $scaleFactor) - $intervals{'start_reel'}))/$scaleFactor);
# Treating current position
$basesCoverred += ($pos * $scaleFactor) - $intervals{'start_reel'};
print "\tCase 2 : \$basesCoverred += ($pos * $scaleFactor) - $intervals{'start_reel'}\n" if $config{'debug'};
# Treating the next full pixels
for (my $i = 0 ; $i < $numPixelImpliy ; $i++){
print "\tCase 2 : \$previousBases[$i] += $scaleFactor\n" if $config{'debug'};
$previousBases[$i] += $scaleFactor;
}
# Treat the last not full pixel
$previousBases[$numPixelImpliy] += (($intervals{'end_reel'} - $intervals{'start_reel'} - (($pos * $scaleFactor) - $intervals{'start_reel'}))%$scaleFactor);
print "\tCase 2 : \$previousBases[$numPixelImpliy] += (($intervals{'end_reel'} - $intervals{'start_reel'} - (($pos * $scaleFactor) - $intervals{'start_reel'}))%$scaleFactor);\n" if $config{'debug'};
}# feature end is on next pixels
print "\tbasesCoverred = $basesCoverred\n" if $config{'debug'};
}# while ($intervalsTabM[0]->[0] < ($pos * $scaleFactor))
print "Stop while\n" if $config{'debug'};
# Compute percentage of base coverage
my $percentage;
if ($rounding_method eq "floor") {$percentage = floor(($basesCoverred/$scaleFactor) * 100);}
elsif ($rounding_method eq "ceil" ) {$percentage = ceil (($basesCoverred/$scaleFactor) * 100);}
print "percentage = $basesCoverred/$scaleFactor = ".($basesCoverred/$scaleFactor)."\n" if $config{'debug'};
print "percentage = $percentage % \n\n" if $config{'debug'};
print "$pos = ${cs}_heatmap$percentage\n" if $config{insaneDebugMode};
# kill if more than 100 %
if ($percentage > 100) {printError("Higher than 100 % ($percentage % )", 1);}
# Draw the current pixel
if ($config{region_file}) {
$$ref_img->filledRectangle($offset{$type}{$strand}{'x'}, $offset{$type}{$strand}{'y'} + $posPic * $win_size,
$offset{$type}{$strand}{'x'} + $strand_width, $offset{$type}{$strand}{'y'} + $posPic * $win_size + $win_size,
$color{"${cs}_heatmap$percentage"});
}
else {
$$ref_img->filledRectangle($offset{$type}{$strand}{'x'}, $offset{$type}{$strand}{'y'} + $pos * $win_size,
$offset{$type}{$strand}{'x'} + $strand_width, $offset{$type}{$strand}{'y'} + $pos * $win_size + $win_size,
$color{"${cs}_heatmap$percentage"});
}
my $st = $pos * $scaleFactor;
my $en = $pos * $scaleFactor + $scaleFactor;
print CSV "$seqName\t$type\t$st\t$en\t$percentage\n";
}# End # For each pixel of the chromosome/sequence
# Draw Centromere
if (defined($centro{start})) {
#Left Triangle
my $poly = new GD::Polygon;
$poly->addPt($offset{$type}{$strand}{'x'} - 1, ($offset{$type}{$strand}{'y'} + $$ref_centromere{start}/$scaleFactor));
$poly->addPt($offset{$type}{$strand}{'x'} - 1, ($offset{$type}{$strand}{'y'} + $$ref_centromere{end} /$scaleFactor));
$poly->addPt(($offset{$type}{$strand}{'x'} + $strand_width/2), ($offset{$type}{$strand}{'y'} + ($$ref_centromere{start}/$scaleFactor + $$ref_centromere{end}/$scaleFactor)/2));
$$ref_img->filledPolygon($poly, $color{$config{'background'}});
#Right Triangle
$poly = new GD::Polygon;
$poly->addPt($offset{$type}{$strand}{'x'} + $strand_width + 1, ($offset{$type}{$strand}{'y'} + $$ref_centromere{start}/$scaleFactor));
$poly->addPt($offset{$type}{$strand}{'x'} + $strand_width + 1, ($offset{$type}{$strand}{'y'} + $$ref_centromere{end} /$scaleFactor));
$poly->addPt(($offset{$type}{$strand}{'x'} + $strand_width/2), ($offset{$type}{$strand}{'y'} + ($$ref_centromere{start}/$scaleFactor + $$ref_centromere{end}/$scaleFactor)/2));
$$ref_img->filledPolygon($poly, $color{$config{'background'}});
}
# Open label group for next rotation
$$ref_img->startGroup("rotate_$randNum");
# Draw label Sequence Name
$$ref_img->string(gdLargeFont,
$offset{$type}{$strand}{'x'} + (($strand_width/2) - (length($seqName) * gdLargeFont->width)/2),
$offset{$type}{$strand}{'y'} - (3 * gdLargeFont->height),
$seqName,
$color{'black'});
# Draw label type
$$ref_img->string(gdLargeFont,
$offset{$type}{$strand}{'x'} + (($strand_width/2) - (length($type) * gdLargeFont->width)/2),
$offset{$type}{$strand}{'y'} - (2 * gdLargeFont->height),
$type,
$color{'black'});
# Close label group for next rotation
$$ref_img->endGroup;
# Draw strand label
$$ref_img->string(gdLargeFont,
$offset{$type}{$strand}{'x'} + (($strand_width/2) - (length($strand) * gdLargeFont->width)/2),
$offset{$type}{$strand}{'y'} - gdLargeFont->height,
$strand,
$color{'black'});
# close chromosome/sequence group
$$ref_img->endGroup;
print "Finish Drawing pixels.\n" if $config{'verbose'};
}
###########################################################################
sub drawPixelsGC{
# Draw each pixel of strand
# Input :
# - $ref_img -> ref of the image
# - $ref_rand -> ref on the hash of random numbers
# - $cs -> colour_scale to use for colors
# - $seqName -> Name of the annotated sequence
# - $chr_size -> Chromosome/Sequence size
# - $scaleFactor -> Scale_factor
# - $win_size -> Size of the printed window in pixel
# - $ref_sequence -> sequence of the chromosome
# Ouput : none
print "Start Drawing pixels GC ...\n" if $config{'verbose'};
my ($ref_img, $ref_rand, $cs, $seqName, $chr_size, $scaleFactor, $win_size, $ref_sequence) = @_;
my $randNum;
# Search a unique random number
while (1) {
$randNum = int rand(1000);
redo if $$ref_rand{$randNum}++;
last;
}
print "\tchr_size = $chr_size\n" if $config{'debug'};
print "\tscaleFactor = $scaleFactor\n" if $config{'debug'};
# Open chromosome/sequence group
$$ref_img->startGroup("+-_${randNum}");
# For each pixel of the chromosome/sequence
my $posPic = 0;
for (my $pos = 0 ; $pos <= $chr_size ; $pos++) {
#Get next window sequence
my $win_seq = substr $$ref_sequence, 0, $scaleFactor, '';
next if ($config{region_file} && (($pos*$scaleFactor) < $region{$seqName}{start} || ($pos*$scaleFactor+$scaleFactor) > $region{$seqName}{end}));
$posPic++;
#Count GC bases
my $gcBases = $win_seq =~ tr/GC//;
# Compute percentage of base coverage
my $percentage;
if ($rounding_method eq "floor") {$percentage = floor(($gcBases/$scaleFactor) * 100);}
elsif ($rounding_method eq "ceil" ) {$percentage = ceil (($gcBases/$scaleFactor) * 100);}
# kill if more than 100 %
if ($percentage > 100) {printError("Higher than 100 % ($percentage % )", 1);}
# Draw the current pixel
if ($config{region_file}) {
$$ref_img->filledRectangle($offset{gc}{'x'}, $offset{gc}{'y'} + $posPic * $win_size,
$offset{gc}{'x'} + $strand_width, $offset{gc}{'y'} + $posPic * $win_size + $win_size,
$color{"${cs}_heatmap$percentage"});
}
else {
$$ref_img->filledRectangle($offset{gc}{'x'}, $offset{gc}{'y'} + $pos * $win_size,
$offset{gc}{'x'} + $strand_width, $offset{gc}{'y'} + $pos * $win_size + $win_size,
$color{"${cs}_heatmap$percentage"});
}
my $st = $pos * $scaleFactor;
my $en = $pos * $scaleFactor + $scaleFactor;
print CSV "$seqName\tGC%\t$st\t$en\t$percentage\n";
}# End # For each pixel of the chromosome/sequence
# Open label group for next rotation
$$ref_img->startGroup("rotate_$randNum");
# Draw label Sequence Name
$$ref_img->string(gdLargeFont,
$offset{gc}{'x'} + (($strand_width/2) - (length($seqName) * gdLargeFont->width)/2),
$offset{gc}{'y'} - (3 * gdLargeFont->height),
$seqName,