forked from HRGV/phyloFlash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phyloFlash_plotscript_svg.pl
executable file
·1771 lines (1610 loc) · 72.7 KB
/
phyloFlash_plotscript_svg.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/env perl
=head1 NAME
phyloFlash_plotscript_svg.pl - Produce SVG-formatted plots for phyloFlash
pipeline
=head1 SYNOPSIS
B<phyloFlash_plotscript_svg.pl> -tree F<<FILE>> -hist F<<FILE>> -bar F<<FILE>> -pie F<<FILE>>
B<phyloFlash_plotscript_svg.pl> -help
B<phyloFlash_plotscript_svg.pl> -man
=head1 DESCRIPTION
Internal script used by B<phyloFlash.pl> to produce SVG-formatted plots for
HTML report file.
Output files are simply input filename with .svg suffix, silently overwritten.
=cut
use strict;
use warnings;
#use diagnostics;
use Pod::Usage;
use POSIX qw (ceil floor);
use List::Util qw(min max);
use Getopt::Long;
use Math::Trig qw(pi cylindrical_to_cartesian);
# Plot insert size histogram and guide tree in SVG format without additional dependencies on R packages
# Input arguments
my ($treefile, $fastafile, $histofile, $barfile, $piefile, $title, $decimalcomma, $pipemode, $assemcov, $unassem_count);
my ($nbreaks, $barminprop) = (undef, 0.2); # Default values for params
my ($plotheight, $plotwidth, $plotcolor);
if (!@ARGV) { # Help msg if no arguments given
pod2usage (2);
exit();
}
GetOptions("tree|t=s" => \$treefile, # Guide tree from MAFFT
"treefasta=s" => \$fastafile, # Fasta file used to produce guide tree - to rewrite the text labels
"assemcov=s" => \$assemcov, # CSV file libNAME.phyloFlash.extractedSSUclassifications.csv
"unassemcount=i" => \$unassem_count, # Number of unassembled reads
"hist|h=s" => \$histofile, # Insert size histogram from BBmap (PE reads only)
"bar|r=s" => \$barfile, # Table of counts to make barplot
"pie|p=s" => \$piefile, # Table of counts to make donut/piechart
"pipe=s" => \$pipemode, # Pipe mode - take input from STDIN and write to STDOUT - specify type of output
"title=s" => \$title, # Title for plot
"height=i" => \$plotheight, # Optional height for plot
"width=i" => \$plotwidth, # Optional width for plot in pixels
"color=s" => \$plotcolor, # Optional fill color for plot (currently implemented only for histogram)
"decimalcomma" => \$decimalcomma,# BBmap is locale-aware and may produce histogram files with decimal comma!
# Perl does not use locales unless requested so the other inputs should be safe
"breakpoints|b=i" => \$nbreaks, # Optional: manually specify number of breakpoints in histogram (e.g. 30)
"help" => sub { pod2usage(1) },
"man" => sub { pod2usage(-exitval=>0, -verbose=>2) },
) or pod2usage(2);
=head1 OPTIONS
=over 15
=item -tree F<<FILE>>
Phylogenetic tree plot from Newick-formatted tree. Does not support node or
branch labels, branch lengths required. Tree is oriented with root on left
and leaf labels on right. Height of plot scales with number of leaves.
Can be modified with -treefasta, -assemcov, and -unassemcount
=item -treefasta F<<FILE>>
Original Fasta file used to produce the tree, if the tree is a guide tree
from MAFFT aligner. This is used to relabel the text labels in the SVG
with the original text, because MAFFT automatically replaces spaces and
other punctuation with underscores, and truncates the names.
=item -assemcov F<<FILE>>
CSV file containing coverage stats of assembled SSU sequences, from phyloFlash
output, for drawing bubbles representing read coverage per assembled sequence
on the tree.
When supplied, beneath the node for each leaf representing an assembled SSU
sequence will be a circle whose area represents the abundance of that sequence
(i.e. number of reads mapping to it in the re-mapping step) in the read library.
=item -unassemcount I<INT>
Number of reads that are not mapping to any of the assembled SSU sequences.
This is to draw a bubble representing coverage of unassembled reads, when
coverage of assembled sequences is also supplied via -assemcov parameter.
=item -hist F<<FILE>>
Histogram plot from TAB-separated histogram output file, e.g. those produced by
BBmap. Column 1: bin values, column 2: counts/frequencies. The counts must
already be binned into counts or frequencies! The counts can be re-binned into
new bins for plotting, but the user is responsible for making sure that the
number of new bins < number of original bins.
=item -bar F<<FILE>>
Interactive bar plot from CSV file. Column 1: label, column 2: counts. The
text labels are adjacent to the bars and their size and opacity are scaled to
the bar height to avoid overlapping text labels. However on mouseover (when
viewed in web browser) the box will be highlighted and the corresponding text
label will enlarge to legible size.
=item -pie F<<FILE>>
Pie chart from CSV file. Column 1: Label, column 2: counts/percentage/ratio.
Will automatically take total of the numbers provided. I.e. if using
percentages, ensure that they add up to 100%.
=item -pipe I<OUTPUT TYPE>
Use "pipe" mode. Input is read from STDIN and written to STDOUT. Use this
option to specify type of plot to produce: "tree", "hist", "bar", or "pie".
Naturally this can only read and write one file at a time.
=item -title="I<STRING>"
Optional title for plot. Enclose string in quotation marks if title has spaces.
Default: (empty)
=item -height I<INT>
=item -width I<INT>
Optional width and height of plot in pixels.
Defaults: Built-in defaults for each plot type.
=item -color="I<STRING>"
Optional fill color for plot. Currently implemented only for histogram.
Default: Built-in defaults for each plot type
=item -breakpoints I<INT>
Specify number of breakpoints in histogram.
Default: Sturges algorithm to calculate optimal breakpoints
=item -decimalcomma
Use comma as decimal separator (only used for histogram input). BBMap is locale-
aware and uses decimal comma in certain locales (e.g. Germany and France).
However, Perl does not unless explicitly requested. This option will replace
decimal commas with decimal periods in histogram input files only.
=item -help
This help message
=item -man
Manual page (identical to this help message)
=back
=cut
## MAIN ########################################################################
my $delim = "comma";
my $dl = defined $delim && $delim eq "tab" ? "\t" : ","; # Check if TSV
if (defined $pipemode) {
# If in pipe mode, take input from STDIN and write to STDOUT
# Get input into array
my @input_arr;
while (<>) {
chomp;
push @input_arr, $_;
}
# Process input depending on specified graphic type
if ($pipemode eq "pie") {
do_pie_or_bar_chart (\@input_arr, "array", $dl, "pie", $title);
} elsif ($pipemode eq "bar") {
do_pie_or_bar_chart (\@input_arr, "array", $dl, "bar", $title);
}
} else {
# Otherwise in "file" mode read and write to specified files
if (defined $histofile) {
do_histogram_plots($histofile, $title, $plotwidth, $plotheight, $plotcolor);
#if (defined $plotheight || defined $plotwidth) {
# # If custom plot height and/or width specified
# do_histogram_plots($histofile, $title, $plotwidth, $plotheight);
#} else {
# # Else use defaults (currently 240 x 240)
# do_histogram_plots($histofile, $title, 240, 240);
#}
}
if (defined $treefile) {
do_phylog_tree($treefile);
}
if (defined $barfile) {
do_pie_or_bar_chart($barfile, "file", $dl, "bar", $title);
}
if (defined $piefile) {
do_pie_or_bar_chart($piefile, "file", $dl, "pie", $title);
}
}
## SUBROUTINES FOR PIECHART ###################################################
sub do_pie_or_bar_chart {
my ($input, $mode, $dl, $type, $title) = @_;
my $in_href;
my $outname;
my $outfh;
# Array mode or infile mode
if ($mode eq "file") {
$in_href = csv2hash($input, $dl);
$outname = $input.".svg";
open ($outfh, ">", $outname) or die ("Cannot open file $outname for writing: $!");
} elsif ($mode eq "array") { # pipe mode
$in_href = csv_arr2hash ($input, $dl);
$outfh = *STDOUT;
}
if ($type eq "pie") {
hash2pie ($in_href, $outfh, $title);
} elsif ($type eq "bar") {
hash2barchart ($in_href, $outfh, $title);
}
close ($outfh) if $mode eq "file";
}
sub pc2xy {
# Convert percentages along a circle to x-y coordinates
my ($pc, # Fractional (0 to 1) position along the circle
$cx, # Center of circle
$cy,
$rad # Radius
) = @_;
# Reminder - offset is counter-clockwise and starts from 3 o'clock
$pc = 1 - $pc; # Account for counter-clockwiseness
# Use functions from Math::Trig
my $theta = 2 * pi * $pc;
my ($xoff, $yoff, $discard) = cylindrical_to_cartesian ($rad, $theta, 0);
my ($x, $y) = ($xoff + $cx, $yoff + $cy); # Offset by circle center
return ($x, $y);
}
sub hash2pie {
my ($csv_href, # Name of input hash
$fh, # Filehandle for output print
$title, # Title text, if available
) = @_;
# Calculate cumulative percentages of input sorted by counts
my $cumul_href = counthash_cumul_sum ($csv_href, 1, "counts");
# Dereference output into arrays for plotting
my @labels_arr = @{$cumul_href->{"labels"}}; # Array of text labels
my @cumul_pc_arr = @{$cumul_href->{"cumul_pc"}}; # Array of cumulative percentages
my @pc_arr = @{$cumul_href->{"pc"}};
my @counts_arr = @{$cumul_href->{"counts"}}; # Array of counts
my @colors_arr = @{$cumul_href->{"color"}} if defined $cumul_href->{"color"}; # Array of colors, if defined
# SVG plot preferences
my $viewBox_width = 240; # width
my $viewBox_height = 240; # height
my $margin = 60;
my $font_size = 14;
# Take shorter dimension, and calculate the pie diameter and circumference
my $diam = $viewBox_width < $viewBox_height ? $viewBox_width : $viewBox_height;
$diam = $diam - 2 * $margin;
my $rad = $diam / 2;
my $cx = $viewBox_width / 2;
my $cy = $viewBox_height / 2;
my $circum = $diam * 3.14159265; # probably precise enough
#my $circum = $diam * 22 / 7; # probably not precise enough
my $stroke_width = 0.75 * $rad; # stroke width is proportion of radius
# Calculate stroke-dasharray and stroke-dashoffset
# Tips: http://openstudio.redhat.com/scratch-made-svg-donut-pie-charts-in-html5/
my @dasharray_arr;
my @dashoffset_arr;
for (my $i=0; $i <= $#labels_arr; $i++) {
push @dashoffset_arr, $cumul_pc_arr[$i] * $circum;
my $stroke = $pc_arr[$i] * $circum;
my $space = $circum - $stroke;
push @dasharray_arr, ("$stroke $space");
}
# SVG parameters
my $viewBox = "0 0 $viewBox_width $viewBox_height";
my $svg_open = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"$viewBox\" height=\"100%\" >\n";
my $donut_std_params = "cx=\"$cx\" ".
"cy=\"$cy\" ".
"r=\"$rad\" ".
"fill=\"transparent\" ".
"stroke-width=\"$stroke_width\" ";
# Start SVG file
print $fh $svg_open;
# Print title
if (defined $title) {
print $fh "<text ".
"style=\"fill:black;font-size:$font_size;text-anchor:middle;font-weight:bold;\" ".
"x=\"".($viewBox_width / 2)."\" ".
"y=\"18\" ".
">".
$title.
"</text>\n";
}
for (my $i=0; $i <= $#labels_arr; $i++) {
my $color;
if (defined $colors_arr[$i]) {
$color = $colors_arr[$i];
} else {
my @rand_colors = (int(rand(256)),int(rand(256)),int(rand(256)));
$color = join (",", @rand_colors);
$color = "rgb(".$color.")";
}
print $fh "<circle class=\"donut-segment\" ".
$donut_std_params.
"stroke=\"$color\" ".
"stroke-dasharray=\"".$dasharray_arr[$i]."\" ".
"stroke-dashoffset=\"".$dashoffset_arr[$i]."\" ".
"></circle>\n";
}
# Print labels in separate loop because they must be on top of donut segments
my $text_style_base = "fill:black;font-size:".$font_size."px;";
for (my $i=0; $i <= $#labels_arr; $i++) {
my ($x_text, $y_text) = pc2xy($cumul_pc_arr[$i] - $pc_arr[$i] / 2,
$cx,
$cy,
$rad);
print $fh "<text ".
"x=\"$x_text\" ".
"y=\"$y_text\" ".
"style=\"".$text_style_base."text-anchor:middle;\" ".
">".
$labels_arr[$i].
"</text>\n";
}
print $fh "</svg>\n";
# Mysteries:
# why does stroke-dashoffset move counterclockwise????!?
# why does SVG y coordinate start from the top????!??
}
### SUBROUTINES FOR BARCHART ##################################################
sub csv2hash {
# CSV file to hash
my ($infile, $delim) = @_;
my %hash;
open (my $fhin, "<", $infile) or die ("Cannot open file $infile for reading: $!");
while (<$fhin>) {
chomp;
my @splitline = split "$delim";
$hash{$splitline[0]} = $splitline[1];
}
close($fhin);
return (\%hash);
}
sub csv_arr2hash {
# CSV array to hash
my ($aref, $delim) = @_;
my %hash;
foreach my $line (@$aref) {
my @splitline = split "$delim", $line;
$hash{$splitline[0]} = $splitline[1];
}
return (\%hash);
}
sub counthash_cumul_sum {
my ($inhref, # Ref to hash of raw counts (keyed by label)
$maxprop, # Maximum cumulative percentile to display taxonomic breakdown (<= 1)
$sortby, # Sort by what? either "counts" or "name"
) = @_;
# Initialize variables
my $minprop = 1 - $maxprop;
my $total = 0;
my %valhash;
# Sort input by either counts or names
my $running_total = 0;
my @sortkey;
if ($sortby eq "counts") {
@sortkey = sort {$inhref->{$b} <=> $inhref->{$a}} keys %$inhref;
} elsif ($sortby eq "name") {
@sortkey = sort {$a cmp $b} keys %$inhref;
}
# Add up total
foreach my $key (@sortkey) {
$total += $inhref->{$key};
}
# Add up cumulative totals
my $index = 0;
my $last_index;
my @labels_arr; # Array of text labels
my @pc_arr; # Array of percentages
my @cumul_pc_arr; # Array of cumulative percentages
my @counts_arr; # Array of raw counts
my $other_count = 0;
for (my $i=0; $i <= $#sortkey; $i++) {
$valhash{$sortkey[$i]}{"raw"} = $inhref->{$sortkey[$i]};
$running_total += $valhash{$sortkey[$i]}{"raw"}; # Update cumulative total
$valhash{$sortkey[$i]}{"cumul"} = $running_total;
# Convert values to fractions of total
$valhash{$sortkey[$i]}{"pc"} = $valhash{$sortkey[$i]}{"raw"} / $total;
$valhash{$sortkey[$i]}{"cumul_pc"} = $valhash{$sortkey[$i]}{"cumul"} / $total;
my $leftover = 1;
$leftover = 1 - $valhash{$sortkey[$i-1]}{"cumul_pc"} if $i > 0; # Some gymnastics
unless ($leftover < $minprop) {
push @labels_arr, $sortkey[$i];
push @pc_arr, $valhash{$sortkey[$i]}{"pc"};
push @cumul_pc_arr, $valhash{$sortkey[$i]}{"cumul_pc"};
push @counts_arr, $valhash{$sortkey[$i]}{"raw"};
} else {
# If there are taxa below minimum cumulative count, add counts to group "other"
$other_count += $valhash{$sortkey[$i]}{"raw"};
}
}
# Add last value for "Other" if it is defined
if ($other_count > 0) {
push @labels_arr, "Other taxa (below threshold)";
push @cumul_pc_arr, 1;
push @pc_arr, 1 - $#cumul_pc_arr;
push @counts_arr, $other_count;
}
# Output is a hash of array references - this is necessary to preserve sort order
my %outhash = ("labels" => \@labels_arr,
"cumul_pc" => \@cumul_pc_arr,
"pc" => \@pc_arr,
"counts" => \@counts_arr);
return (\%outhash);
}
sub hash2barchart {
# Read input
my ($csv_href, # Input CSV file
$fh, # Filehandle for output print
$title, # Optional title
) = @_;
# Set preferences
my $maxprop = 1; # Maximum cumulative percentile to display taxonomic breakdown
my $minprop = 1 - $maxprop;
# SVG plot preferences
my $orientation = "v"; # Horizontal (h) or vertical (v) alignment of figure long axis
my $box_proportion = 0.10; # Proportion of figure viewbox occupied by bar vs. text
my $viewBox_longaxis = 300; # Long dimension of the viewbox (parallel to main axis)
my $viewBox_shortaxis = 350; # Short dimension of the viewbox (perpendicular to main axis)
my $margin = 5;
# Calculate cumulative percentages of input sorted by counts
my $cumul_href = counthash_cumul_sum ($csv_href, $maxprop, "counts");
# Dereference output into arrays for plotting
my @labels_arr = @{$cumul_href->{"labels"}}; # Array of text labels
my @x1_arr = @{$cumul_href->{"cumul_pc"}}; # Array of values for right side of bars
my @counts_arr = @{$cumul_href->{"counts"}}; # Array of counts
# Generate array of values for left side of bars
my @x0_arr = @x1_arr;
pop @x0_arr;
unshift @x0_arr, 0;
my @widths_arr;
my @y0_arr = (0) x scalar @x0_arr;
my @y1_arr = (1) x scalar @x1_arr;
# SVG plot parameters
my @boxval = (0, 1, 0, 1);
my $viewBox;
my @box;
if ($orientation eq "h") { # for horizontal bars
$viewBox = "0 0 $viewBox_longaxis $viewBox_shortaxis"; # x y width height
@box = ($margin,
$viewBox_longaxis - $margin,
$viewBox_shortaxis - $viewBox_shortaxis*$box_proportion + $margin,
$viewBox_shortaxis - $margin
); # left right bottom top coordinates
} else {
# for vertical bars
$viewBox = "0 0 $viewBox_shortaxis $viewBox_longaxis"; # x y width height
@box = ($margin,
$viewBox_shortaxis*$box_proportion + $margin,
$margin,
$viewBox_longaxis - $margin
); # left right bottom top coordinates
}
my @viewBox_arr = split " ", $viewBox;
# Convert to coordinates
my ($x0_rescale_aref, $y0_rescale_aref, $x1_rescale_aref, $y1_rescale_aref);
if ($orientation eq "h") {
($x0_rescale_aref, $y0_rescale_aref) = val2coord ($viewBox, \@box, \@boxval, \@x0_arr, \@y0_arr);
($x1_rescale_aref, $y1_rescale_aref) = val2coord ($viewBox, \@box, \@boxval, \@x1_arr, \@y1_arr);
} else {
@x0_arr = map { 1 - $_ } @x0_arr; # Flip coordinates so that most abundant taxon appears on top
@x1_arr = map { 1 - $_ } @x1_arr;
($x0_rescale_aref, $y0_rescale_aref) = val2coord ($viewBox, \@box, \@boxval, \@y0_arr, \@x1_arr);
($x1_rescale_aref, $y1_rescale_aref) = val2coord ($viewBox, \@box, \@boxval, \@y1_arr, \@x0_arr);
}
# Create rectangle values
my %rect_vals;
for (my $i=0; $i <= $#labels_arr; $i++) {
$rect_vals{$i}{"label"} = $labels_arr[$i];
$rect_vals{$i}{"x"} = $x0_rescale_aref->[$i];
$rect_vals{$i}{"width"} = abs ($x1_rescale_aref->[$i] - $x0_rescale_aref->[$i]);
$rect_vals{$i}{"y"} = $y1_rescale_aref->[$i];
$rect_vals{$i}{"height"} = abs($y0_rescale_aref->[$i] - $y1_rescale_aref->[$i]);
$rect_vals{$i}{"counts"} = $counts_arr[$i];
$rect_vals{$i}{"id"} = "rect$i"; # ID of rect object in SVG file
}
my $svg_open = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"$viewBox\" width=\"100%\" height=\"100%\">\n";
#my $style_base = "fill-opacity:0.5;stroke:rgb(0,0,0);stroke-width:1;"; # Style for histogram bars
my $style_base = "fill-opacity:0.5;".
#"stroke:rgb(0,0,0);".
"stroke-width:1;";
print $fh $svg_open;
# Print a title
print $fh "<text ".
"style=\"fill:black;text-anchor:middle;font-weight:bold;font-size:10px;\" ".
"x=\"".($viewBox_arr[2]/2)."\" ".
"y=\""."14"."\" ".
">".
$title.
"</text>\n";
foreach my $rect (sort {$a <=> $b} keys %rect_vals) {
my @bar_colors;
if ($rect_vals{$rect}{"label"} eq "Other taxa (below threshold)") {
@bar_colors = (155,155,155); # "Other taxa" should appear grey
} else {
# Named taxa have nice colors
@bar_colors = (int(rand(256)),int(rand(256)),int(rand(256)));
}
my $bar_color="fill:rgb(".join(",",@bar_colors).");";
my $style=$style_base.$bar_color;
print $fh "<rect ".
"id=\"".$rect_vals{$rect}{"id"}."\" ".
"x=\"".$rect_vals{$rect}{"x"}."\" ".
"y=\"".$rect_vals{$rect}{"y"}."\" ".
"width=\"".$rect_vals{$rect}{"width"}."\" ".
"height=\"".$rect_vals{$rect}{"height"}."\" ".
"style=\"$style\" ".
"onmouseover=\"evt.target.setAttribute('stroke','red');\" ". # Surrounded by red border on mouseover
"onmouseout=\"evt.target.setAttribute('stroke','none');\" ".
"/>\n";
# Print taxonomy string label
my ($x_text, $y_text);
my ($x_text_counts, $y_text_counts);
my $font_size = 8;
#my $text_style_base = "fill:black;font-size:".$font_size."px;";
my $text_style_base = "fill:black;";
my $text_style;
# If bars are too narrowly spaced, make text more transparent and
# smaller, but expand size and grow opaque on mouseover of bars for clarity
# Tips from: http://www.petercollingridge.co.uk/data-visualisation/mouseover-effects-svgs
# Optional params to make text smaller font size if bars too narrowsly spaced
my $mouseover = "<set attributeName=\"font-size\" ".
"to=\"$font_size\" ".
"begin=\"".$rect_vals{$rect}{"id"}.".mouseover\" ".
"end=\"".$rect_vals{$rect}{"id"}.".mouseout\" ".
"/>";
# ALL text labels set to fully opaque on mouseover of bar
my $attr_text = "<set attributeName=\"fill-opacity\" ".
"to=\"1\" ".
"begin=\"".$rect_vals{$rect}{"id"}.".mouseover\" ".
"end=\"".$rect_vals{$rect}{"id"}.".mouseout\" ".
"/>";
my $text_test_dimension;
if ($orientation eq "h") {
$text_style = $text_style_base."writing-mode:tb;";
$x_text = $margin + $rect_vals{$rect}{"x"} + $rect_vals{$rect}{"width"}/2;
$y_text = $margin + $rect_vals{$rect}{"y"} + $rect_vals{$rect}{"height"} + $font_size;
$x_text_counts = $x_text;
$y_text_counts = $margin + $rect_vals{$rect}{"y"} + $rect_vals{$rect}{"height"}/2;
$text_test_dimension = $rect_vals{$rect}{"width"};
} elsif ($orientation eq "v") {
$text_style = $text_style_base;
$x_text = $margin + $rect_vals{$rect}{"width"} + $font_size;
$y_text = $margin + $rect_vals{$rect}{"y"} + $rect_vals{$rect}{"height"} / 2;
$x_text_counts = $margin + $rect_vals{$rect}{"width"} / 2;
$y_text_counts = $y_text;
$text_test_dimension = $rect_vals{$rect}{"height"};
}
# Check if bars are too narrow
if ($text_test_dimension < $font_size) {
# If bars are too narrow, set text labels to be shorter/thinner
$attr_text = $attr_text.$mouseover;
my $new_opacity = 0.5 * ($text_test_dimension / $font_size);
$text_style = $text_style."fill-opacity:$new_opacity;";
$text_style = $text_style."font-size:".$text_test_dimension."px;";
} else {
# All text labels are of 0.6 opacity to begin with, for animation
# to be more consistent to the eye
$text_style = $text_style."fill-opacity:0.6;";
$text_style = $text_style."font-size:".$font_size."px;";
}
print $fh "<text ".
"x=\"$x_text\" ".
"y=\"$y_text\" ".
"style=\"$text_style"."text-anchor:start;\" ".
">".
$attr_text. # Attribute effects on mouseover
$rect_vals{$rect}{"label"}.
"</text>\n";
# Print counts labels
print $fh "<text ".
"x=\"$x_text_counts\" ".
"y=\"$y_text_counts\" ".
"style=\"$text_style;text-anchor:middle;\" ".
">".
$attr_text. # Attribute effects on mouseover
$rect_vals{$rect}{"counts"}.
"</text>\n";
}
print $fh "</svg>\n";
}
### SUBROUTINES FOR HISTOGRAM #################################################
sub do_histogram_plots {
# Get input parameters
my ($infile, # Input filename
$title, # Optional title
$width, # Optional SVG plot width - passed to viewBox
$height, # Optional SVG plot height -passed to viewBox
$color, # Optional SVG fill color
) = @_;
# Check if options defined, else substitute defaults
$width = defined $width ? $width : 240;
$height = defined $height ? $height : 240;
$color = defined $color ? $color : "rgb(155,155,155)";
# SVG and Plot parameters for histograms
my @viewBox_arr = (0, 0, $width, $height); # Viewbox parameter for SVG header - x y width height
my $viewBox = join " ", @viewBox_arr;
my $svg_open = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"$viewBox\" width=\"100%\" height=\"100%\">\n";
# Bounding box coordinates for plot area
my @box_coords = (40, # Left margin - leave space for labels
$width - 20, # Right margin
20, # Bottom margin
$height - 40, # Top margin - leave space for title
); #
my $fill_style = "fill:$color;fill-opacity:0.5;stroke:none"; # Style for histogram bars
# Plot histogram
my $infile_out = "$infile.svg"; # Append .svg to get output file name
my %histo_hash; # Define hash to hold data
read_hist($infile, \%histo_hash); # Read histogram file into memory
open (my $histo_fh, ">", $infile_out) # Open file for printing
or die ("Cannot write to output file $infile_out: $!");
print $histo_fh $svg_open; # Print SVG header
if (defined $title) { # Print title if defined
print $histo_fh "<text ".
"style=\"fill:black;font-size:14px;text-anchor:middle;font-weight:bold;\" ".
"x=\"".($viewBox_arr[2]/2)."\" ".
"y=\"18\" ".
">".
$title.
"</text>\n";
}
draw_histogram ($viewBox, \@box_coords, \%histo_hash, $fill_style, $histo_fh, $nbreaks);
print $histo_fh "</svg>\n"; # Closing SVG tag
close ($histo_fh); # Close file
}
sub draw_histogram {
my ($viewBox, # Viewbox string for SVG tag
$box_aref, # Ref to array (left right bottom top) of bounding box coords
$href, # Ref to histogram to draw
$style, # Style string for rect element
$handle, # Print output handle
$nbreaks # Number of breaks (if undefined, default to Sturges)
) = @_;
my ($vb_x, $vb_y, $vb_width, $vb_height) = split " ", $viewBox;
my ($left, $right, $bottom, $top) = @$box_aref; # dereference bounding box (coord space)
# Refactor histogram to new breaks
my ($breaks_aref, $counts_aref) = lump_hist($href, $nbreaks);
# Define plot area (VAL space)
my $x_range_width = max (@$breaks_aref) - min (@$breaks_aref);
my $y_range_width = max (@$counts_aref) - min (@$counts_aref);
my @x_range = (min (@$breaks_aref), max (@$breaks_aref));
my @y_range = (min (@$counts_aref), max (@$counts_aref));
my @boxval = (@x_range, @y_range);
# Rescale coordinates to fit bounding box
my ($breaks_rescale_aref, $counts_rescale_aref) = val2coord($viewBox, $box_aref, \@boxval, $breaks_aref, $counts_aref);
# Calculate parameters for rect elements
my $rect_params_href = rect_params($viewBox, $box_aref, $breaks_rescale_aref, $counts_rescale_aref);
# Print rect elements
foreach my $rect (sort {$a <=> $b} keys %$rect_params_href) {
print $handle "<rect ".
"x=\"".$rect_params_href->{$rect}{"x"}."\" ".
"y=\"".$rect_params_href->{$rect}{"y"}."\" ".
"width=\"".$rect_params_href->{$rect}{"width"}."\" ".
"height=\"".$rect_params_href->{$rect}{"height"}."\" ".
"style=\"$style\" ".
"/>\n";
}
# Axis tick marks - should have ca. ten tick marks
my @xax_ticks = tick_intervals(min(@$breaks_aref), max(@$breaks_aref));
my @yax_ticks = tick_intervals(min(@$counts_aref), max(@$counts_aref),15);
# Rescale to the bounding box coordinates
my ($xax_ticks_rescale_aref, $yax_ticks_rescale_aref) = val2coord ($viewBox, $box_aref, \@boxval, \@xax_ticks, \@yax_ticks);
# Axis style and positions
my $tick_style = "stroke:rgb(0,0,0);stroke-width:1";
#my $xax_pos = ${$box_aref}[3] - ${$box_aref}[2]; # top - bottom
my $xax_pos = $vb_height - ${$box_aref}[2];
my $yax_pos = ${$box_aref}[0]; # left
svg_axis_ticks($xax_ticks_rescale_aref, # Horizontal axis
\@xax_ticks,
$xax_pos,
"h",
$tick_style,
$handle);
svg_axis_ticks($yax_ticks_rescale_aref, # Vertical axis
\@yax_ticks,
$yax_pos,
"v",
$tick_style,
$handle);
}
sub svg_axis_ticks {
my ($ticks_aref, # Ref to array of tickmark positions (rescaled to coord sys)
$ticks_vals_aref, # Ref to array of tickmark values
$axis, # Axis position
$orientation, # "h" for horizontal, "v" for vertical
$style, # String for SVG style param
$handle # Print output handle
)= @_;
my @ticks = @$ticks_aref; # Dereference array Ref
my ($x1, $x2, $y1, $y2);
if ($orientation eq "h") { # If horizontal axis...
$x1 = $ticks[0];
$x2 = $ticks[$#ticks];
$y1 = $axis;
$y2 = $axis;
} else {
$x1 = $axis;
$x2 = $axis;
$y1 = $ticks[0];
$y2 = $ticks[$#ticks];
}
print $handle "<line ".
"x1=\"$x1\" ".
"x2=\"$x2\" ".
"y1=\"$y1\" ".
"y2=\"$y2\" ".
"style=\"$style\" ".
"/>\n";
# Tick marks and text labels
for (my $j=0; $j <= $#ticks; $j++) {
my ($x1, $x2, $y1, $y2);
if ($orientation eq "h") {
$x1 = $ticks[$j];
$x2 = $ticks[$j];
$y1 = $axis;
$y2 = $axis + 2.5; # Magic number
} else {
$x1 = $axis;
$x2 = $axis - 2.5; # Magic number
$y1 = $ticks[$j];
$y2 = $ticks[$j];
}
print $handle "<line ".
"x1=\"$x1\" ".
"x2=\"$x2\" ".
"y1=\"$y1\" ".
"y2=\"$y2\" ".
"style=\"$style\" ".
"/>\n";
# Text label
unless ($j==0) { # Labels for horizontal axis
my ($x_text, $y_text, $text_anchor);
if ($orientation eq "h") {
$x_text = $x1;
$y_text = $y2 + 9; # Magic number
$text_anchor = "middle";
} else { # Labels for vertical axis
$x_text = $x2 - 2; # Magic number
$y_text = $y1;
$text_anchor = "end";
}
print $handle "<text ".
"x=\"$x_text\" ".
"y=\"$y_text\" ".
"text-anchor=\"$text_anchor\" ".
"fill=\"black\" ".
"style=\"fill:black;font-size:8px;\"".
">";
print $handle ${$ticks_vals_aref}[$j]; # Text of label
print $handle "</text>\n";
}
}
}
sub tick_intervals { # Value space
# Determine intervals for tick marks of an axis, given the min and max vals
# should have ca. ten tick marks, and also scale with the plot height
my ($min, # Minimum value for axis
$max, # Maximum value for axis
$max_ticks, # Optional maximum number of tick marks
) = @_;
# Interval is based on first significant digit
my $int = 10**(floor(log($max - $min)/log(10) - 0.5));
# Generate tick marks, with some buffer on ends
my @ticks = ceil($min/$int + 0.5) .. floor($max/$int - 0.5);
# If there are more than ten tick marks, redo intervals
if (defined $max_ticks && scalar @ticks > $max_ticks) {
$int = 10**(floor(log($max - $min)/log(10) - 0.5) + 1);
@ticks = ceil($min/$int + 0.5) .. floor($max/$int - 0.5);
}
@ticks = map {$_ * $int} @ticks;
# Add min and max values to the tick marks
unshift @ticks, $min;
push @ticks, $max;
# Return result
return (@ticks);
}
sub read_hist {
# Read histogram from TSV formatted file and write into a hash
# keys - column 1
# values - column 2 (no. observations)
my ($file, $href) = @_;
open(my $fhin, "<", $file) or die ("Cannot open $file: $!");
while (<$fhin>) {
chomp;
unless (m/^#/) { # Skip comment lines
my @splitline = split /\t/;
my ($col1, $col2) = ($splitline[0], $splitline[1]);
if (defined $decimalcomma) {
$col1 =~ s/,/\./;
$col2 =~ s/,/\./;
}
$href->{$col1} = $col2 unless $col2 == 0; # Skip zeroes
}
}
close($fhin);
}
sub hist_min_max_n { # VALUE SPACE
# Give min and max values of histogram, and total n of observations, from
# histogram hash produced by read_hist
my ($href) = @_;
my @vals = (sort {$a <=> $b} keys %$href);
my ($min, $max) = ($vals[0], $vals[$#vals]);
my $total=0;
foreach my $val (@vals) {
$total+= $href->{$val};
}
return ($min, $max, $total);
}
sub numbreaks_sturges { # VALUE SPACE
# Calculate no. of breaks in histogram from total length by formula of
# Sturges (default in R hist function)
my ($x) = @_;
my $out = ceil(log($x)/log(2) + 1);
return $out;
}
sub breaks_hist { # VALUE SPACE
# Calculate break points in histogram, from hash produced by read_hist
my ($href, # Ref to hash produced by read_hist
$nbreaks_user # Number of breaks (leave blank to use default Sturges)
) = @_;
my ($min, $max, $n) = hist_min_max_n ($href);
my $nbreaks;
if (defined $nbreaks_user) {
$nbreaks = $nbreaks_user;
} else {
$nbreaks = numbreaks_sturges($n);
}
my $int = ($max - ($min-1))/$nbreaks;
my @ints;
for (my $i=0; $i <= $nbreaks; $i++) {
push @ints, floor($i*$int + ($min-1)); # Round to nearest integer
}
return (@ints);
}
sub lump_hist { # VALUE SPACE
# Refactor histograms to fit new breakpoints
my ($href, # Ref to hash produced by read_hist
$nbreaks_user # Number of breaks (leave blank to use default Sturges)
) = @_;
my %out;
my @breaks = breaks_hist($href, $nbreaks_user);
my @counts = (0) x $#breaks;
for (my $i=1; $i <= $#breaks; $i++) {
foreach my $val (keys %$href) {
if ($val <= $breaks[$i] && $val > $breaks[$i-1]) {
$counts[$i-1]+= $href->{$val};
}
}
}
return (\@breaks, # Breakpoints for histogram
\@counts # Counts in each bin
);
}
sub val2coord { # VALUE TO COORD SPACE
# Rescale xy-values to xy-coordinates for SVG
# taking into account the fact that SVG counts y coords from top of box
my ($viewBox, # viewBox string (space-separated values)
$box_aref, # Reference to bounding box array (left right bottom top)
# representing coordinates of plot area in SVG coords
$boxval_aref, # Reference to bounding box array (left right bottom top)
# but now in terms of xy-values
$xvals_aref, # Reference to array of x-values to be converted
$yvals_aref, # Reference to array of y-values to be converted
) = @_;
# Dereference arrays
my ($vb_x, $vb_y, $vb_width, $vb_height) = split / /, $viewBox;
my ($cleft, $cright, $cbottom, $ctop) = @$box_aref;
my ($vleft, $vright, $vbottom, $vtop) = @$boxval_aref;
my @xvals = @$xvals_aref;
my @yvals = @$yvals_aref;
my $cwidth = $cright - $cleft;
my $cheight = $ctop - $cbottom;
my $vwidth = $vright - $vleft;
my $vheight = $vtop - $vbottom;
my @xcoords = map {$cleft + ($_ - $vleft ) * ($cwidth / $vwidth)} @xvals;
my @ycoords = map {$vb_height - $cbottom - ($_ * ($cheight / $vheight))} @yvals;
return (\@xcoords, \@ycoords);
}
sub rect_params { # COORD SPACE
my ($viewBox,
$box_aref,
$breaks_aref, # Array of breaks
$counts_aref # Array of counts - length should be one less than breaks
) = @_;
my @viewBox_arr = split " ", $viewBox;
my ($left, $right, $bottom, $top) = @$box_aref;
my @breaks = @$breaks_aref;
my @counts = @$counts_aref;
my %rect_params;
for (my $i=0; $i <= $#counts; $i++) {
$rect_params{$i}{"x"} = $breaks[$i];
$rect_params{$i}{"y"} = $counts[$i];
$rect_params{$i}{"width"} = $breaks[$i+1] - $breaks[$i];
$rect_params{$i}{"height"} = $viewBox_arr[3] - $counts[$i] - $bottom;
}
return (\%rect_params);
}
## SUBROUTINES FOR TREE #######################################################
sub do_phylog_tree { # Global vars
my ($infile) = @_;
my @treearr; # Array to store lines of tree
open(my $fhin, "<", $infile) or die ("Cannot open for reading $infile: $!");
while (<$fhin>) {
chomp;
push @treearr, $_;
}
close($fhin);
my $treefile_out = "$infile.svg"; # Append .svg suffix to infile name for output
my $treestr = join "", @treearr; # Concatenate all lines of Newick file into a single string
draw_tree($treestr, $treefile_out);
}
sub draw_tree {
my ($treestr, # String (no linebreaks) containing Newick-formatted tree
$outfile, # Name of output file
) = @_;
# Parse Newick file to get tree node info
my ($nodes_href, $taxa_href) = newick2tables ($treestr);
#dump_node_data($nodes_href); # diagnostics
#dump_taxon_data($taxa_href); # diagnostics
# SVG and Plot parameters for tree
my ($vb_x, $vb_y, $vb_width, $vb_height) = (0, 0, 600, 200);
# Count number of taxa to calculate image height
my $num_taxa = scalar (keys %$taxa_href);
if ($num_taxa > 10) { # Make image larger if number of taxa is large
$vb_height = 30*$num_taxa;
}