Debian Codegraph @

misc/plot-ampliconstats

raw
1 #!/usr/bin/env perl
2 #
3 # Copyright (C) 2020-2021 Genome Research Ltd.
4 #
5 # Author: James Bonfield <[email protected]>
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 # DEALINGS IN THE SOFTWARE.
24
25 # Note: this script requires gnuplot version 5.0 or later.
26
27 use strict;
28 use Getopt::Long;
29 use List::Util qw(max);
30 use POSIX qw(ceil);
31
32 my $version = "1.0";
33
34 #-----------------------------------------------------------------------------
35 # Command line argument handling & help
36
37 my %opts;
38
39 my $res = GetOptions("size=s" => \$opts{size},
40 "size2=s" => \$opts{size2},
41 "size3=s" => \$opts{size3},
42 "help" => \$opts{help},
43 "page=i" => \$opts{page},
44 "amp_add=i" => \$opts{amp_add},
45 "amp-add=i" => \$opts{amp_add},
46 "orient=s" => \$opts{orient},
47 "depth_max=s" => \$opts{depth_max},
48 "depth-max=s" => \$opts{depth_max},
49 "thumbnails" => \$opts{thumbnails},
50 "thumb_size=i"=> \$opts{thumb_size},
51 "thumb-size=i"=> \$opts{thumb_size},
52 "index-only" => \$opts{index_only});
53
54 $opts{size} = "1000,800" unless $opts{size};
55 $opts{size2} = "1000,400" unless $opts{size2};
56 $opts{size3} = "400,847" unless $opts{size3};
57 $opts{page} = 96 unless $opts{page};
58 $opts{amp_add} = 100 unless $opts{amp_add};
59 $opts{orient} = "h" unless $opts{orient};
60 $opts{depth_max} = 0 unless $opts{depth_max};
61 $opts{thumbnails}= 0 unless $opts{thumbnails};
62 $opts{thumb_size}= 200 unless $opts{thumb_size};
63
64 my $max_depth = 0;
65
66 my $usage = <<END;
67 plot-ampliconstats version $version
68
69 Usage: plot-ampliconstats prefix [FILE]
70
71 Options:
72 -help Show this usage
73 -size W,H Set image width to W and height to H for heatmaps
74 -size2 W,H Set image width to W and height to H for graphs (horizontal)
75 -size3 W,H Set image width to W and height to H for graphs (vertical)
76 -page N Maximum number of samples per page in heatmaps
77 -amp-add X Small sample fudge: NErr/(NAll+X) in amplicon count plots
78 -orient h/v Orientation for plots, defaults to h (horizontal)
79 -depth-max N Force -reads.png plots to have a fixed yrange
80 -thumbnails Produce scaled down thumbnail images
81 -thumb-size N Display thumbnails as N pixels wide.
82
83 If FILE is not specified, reads from stdin.
84 END
85
86 if ($res != 1 or $opts{help} or scalar(@ARGV) < 1) {
87 print $usage;
88 exit 0;
89 }
90
91 my $prefix = shift(@ARGV);
92
93 # Useful for debugging, but undocumented
94 goto index_only if ($opts{index_only});
95
96 #-----------------------------------------------------------------------------
97 # Check gnuplot version.
98 #
99 # We need >= 5.0 in order to cope with "matrix rowheaders" command.
100 my $g=`gnuplot --version` || die "gnuplot: $!";
101 $g=~m/gnuplot ([\d.]+)/;
102 if ($1 < 5) {
103 print STDERR "Plot-ampliconstats needs gnuplot version 5.0 or later (found $1)\n";
104 exit 1;
105 }
106
107 #-----------------------------------------------------------------------------
108 # Load plot meta-data
109 # This allows us to auto-scale plots.
110 my $namp = 0; # total number
111 my %namp = (); # number per reference
112 my $nfile = 0;
113 my %ref_len = ();
114 my $multi_ref = 0; # Flag for multi-ref mode. Autodetected.
115 my $ref = "_";
116 my %ref_start = (); # cumulative position in genome
117 my $total_len = 0;
118
119 while (<>) {
120 chomp($_);
121 my @F = split("\t", $_);
122 if (/^SS\tNumber of amplicons/) {
123 $multi_ref = 1 if (scalar(@F) > 3);
124 $ref = $F[2];
125 }
126 if (/^SS\tNumber of amplicons/) {
127 $namp{$ref} = $F[2+$multi_ref];
128 $namp += $namp{$ref};
129 }
130 $nfile = $F[2] if (/^SS\tNumber of files/);
131 if (/^SS\tReference length/) {
132 $ref_len{$ref} = $F[2+$multi_ref];
133 $ref_start{$ref} = $total_len;
134 $total_len += $ref_len{$ref};
135 }
136
137 last if (/^SS\tEnd of summary/);
138 }
139
140 #-----------------------------------------------------------------------------
141 # Open up our various gnuplot output files
142
143 # TODO: When nfile is too large we will need to paginate and produce
144 # multiple plots in a for loop. This may change the structure of this
145 # code with the headers being printed inline with the processing loop
146 # below.
147
148 my $xfont = ($namp >= 100) ? 8 : 13;
149 my $yfont = ($nfile >= 80) ? 5 : 8;
150 my $impw1 = ($namp >= 100) ? 4 : 5;
151 my $impw2 = ($namp >= 100) ? 2 : 4;
152
153 #--- Combined
154 open(my $ch_depth, ">", $prefix."-combined-depth.gp") ||
155 die "$prefix-combined-depth.gp";
156
157 open(my $ch_reads, ">", $prefix."-combined-reads.gp") ||
158 die "$prefix-combined-reads.gp";
159
160 open(my $ch_rperc, ">", $prefix."-combined-read-perc.gp") ||
161 die "$prefix-combined-read-perc.gp";
162 if ($opts{orient} eq "h") {
163 print $ch_rperc <<"END";
164 set title "Distribution percentage of reads across amplicons, all files"
165 set xlabel "amplicon"
166 set xrange [0:$namp+1]
167 set ylabel "percentage of reads"
168 set yrange [0:*]
169 set key below
170 set title font "helvetica,20"
171 set xtics 5 font "helvectica,$xfont" scale -2,-1
172 set mxtics 5
173 set terminal png size $opts{size2}
174 set output "$prefix-combined-read-perc.png"
175 plot "-" using (column(0)+1):1 with impulses lw $impw1 title "mean",\\
176 "-" using (column(0)+1):1 with impulses lw $impw2 lt 3 title "s.d."
177 END
178 } else {
179 print $ch_rperc <<"END";
180 set title "Read distribution, all files"
181 set ylabel "amplicon"
182 set yrange [0:$namp+1]
183 set xlabel "percentage of reads"
184 set xrange [0:*]
185 set grid; # xtics noytics
186 set key below
187 set title font "helvetica,20"
188 set ytics 5 font "helvectica,$xfont" scale -2,-1
189 set mytics 5
190 set terminal png size $opts{size3}
191 set output "$prefix-combined-read-perc.png"
192 plot "-" using (0):(column(0)+1):1:(0) with vector nohead lw $impw1 title "mean",\\
193 "-" using (0):(column(0)+1):1:(0) with vector nohead lw $impw2 lt 3 title "s.d."
194 END
195 }
196
197 open(my $ch_amp, ">", $prefix."-combined-amp.gp") ||
198 die "$prefix-combined-amp.gp";
199 if ($opts{orient} eq "h") {
200 print $ch_amp <<"END";
201 set title "Percentage of read-pairs with incorrectly positioned primers, all files"
202 set xlabel "amplicon"
203 set xrange [0:$namp+1]
204 set ylabel "percentage of reads"
205 set yrange [0:*]
206 unset key
207 set title font "helvetica,20"
208 set xtics 5 font "helvectica,$xfont" scale -2,-1
209 set mxtics 5
210 set terminal png size $opts{size2}
211 set output "$prefix-combined-amp.png"
212 plot "-" using (column(0)+1):1 with impulses lw $impw1
213 END
214 } else {
215 print $ch_amp <<"END";
216 set title "% mis-priming, all files"
217 set ylabel "amplicon"
218 set yrange [0:$namp+1]
219 set xlabel "percentage of reads"
220 set xrange [0:*]
221 unset key
222 set grid
223 set title font "helvetica,20"
224 set ytics 5 font "helvectica,$xfont" scale -2,-1
225 set mytics 5
226 set terminal png size $opts{size3}
227 set output "$prefix-combined-amp.png"
228 plot "-" using (0):(column(0)+1):1:(0) with vector nohead lw $impw1
229 END
230 }
231
232 my $fh_reads;
233 my $fh_reads_count = 0;
234 my $fh_reads_page = 1;
235 my $fh_reads_nfile = 0;
236
237 my $fh_depth;
238 my $fh_depth_count = 0;
239 my $fh_depth_page = 1;
240 my $fh_depth_nfile = 0;
241
242 my $fh_amp;
243 my $fh_amp_count = 0;
244 my $fh_amp_page = 1;
245 my $fh_amp_nfile = 0;
246
247 my ($fh_rperc, $fh_rperl);
248 my $fh_rperc_count = 0;
249 my $fh_rperc_page = 1;
250 my $fh_rperc_nfile = 0;
251
252 my %fh_cover; # For coverage at multiple different depths
253 my %fh_cover_count;
254 my %fh_cover_page;
255 my %fh_cover_nfile;
256
257 my @amp_start; # coords for amplicons; cumulative across all refs
258 my @amp_end;
259 my @amp2ref; # map amplicon number to ref name
260
261 #-----------------------------------------------------------------------------
262 # Parse stats file, writing to a number of outputs simultaneously
263 my %file;
264 my $fh;
265 my @combined_coord;
266 while (<>) {
267 chomp($_);
268 my @F = split("\t", $_);
269
270 # Amplicon coordinates
271 if (/^AMPLICON/) {
272 my $min_left=1e9;
273 $ref = $F[1] if $multi_ref;
274 foreach (split(",",$F[2+$multi_ref])) {
275 /\d+-(\d+)/;
276 $min_left=$1 if ($min_left > $1);
277 }
278 my $max_right=0;
279 foreach (split(",",$F[3+$multi_ref])) {
280 /(\d+)-\d+/;
281 $max_right=$1 if ($max_right < $1);
282 }
283 $amp_start[$F[1+$multi_ref]]=$min_left + $ref_start{$ref};
284 $amp_end[$F[1+$multi_ref]]=$max_right + $ref_start{$ref};
285 $amp2ref[$F[1+$multi_ref]] = $multi_ref ? $F[1] : "_";
286
287 $ref_len{$ref} = $max_right if ($ref_len{$ref} < $max_right);
288 }
289
290 # Heatmaps showing all files & all amplicons
291 local $"="\t";
292 my $name=$F[1];
293 $name =~ s/_/\\\\_/g if (/^F/);
294 $name =~ s/"/\\\\"/g if (/^F/);
295
296 # Initialise gnuplot files as we create new pages
297 #---------- Heatmap READS
298
299 # Input order is:
300 # TYPE file1 ref1
301 # TYPE file1 ref2
302 # TYPE file2 ref1
303 # TYPE file2 ref2
304 #
305 # Heatmaps combine file1/2 together, but the ref1/2 are interleaved.
306 # This means we need to load the file contents first so we can consume
307 # in another order.
308
309 $fh_reads_count++ if (/^FREADS/);
310 if (($fh_reads_count > $opts{page} || 0) && $nfile-$opts{page}*$fh_reads_page!=1) {
311 if (defined($fh_reads)) {
312 print $fh_reads "\nend\n";
313 close($fh_reads);
314 system("gnuplot", "$prefix-heat-reads-$fh_reads_page.gp") && die;
315 undef $fh_reads;
316 }
317 $fh_reads_page++;
318 $fh_reads_count = 0;
319 }
320
321 if (/^FREADS/ && !defined($fh_reads)) {
322 $fh_reads_nfile = $fh_reads_page * $opts{page} > $nfile
323 ? $nfile - ($fh_reads_page-1) * $opts{page}
324 : $opts{page};
325 $fh_reads_nfile++ if ($fh_reads_page * $opts{page} == $nfile-1);
326 open($fh_reads, ">", $prefix."-heat-reads-$fh_reads_page.gp") ||
327 die "$prefix-heat-reads-$fh_reads_page.gp";
328 print $fh_reads <<"END";
329 set title "average number of log10(reads) per amplicon, page $fh_reads_page"
330 unset key
331 set xrange [:$namp+1]
332 set yrange [-1:$fh_reads_nfile]
333 set bmargin at screen 0.07
334 set tmargin at screen 0.91
335 #set palette rgbformula 15,4,9
336 set palette rgbformula 32,31,30
337 set cbrange [0:6]
338 set title font "helvetica,20"
339 set xtics 5 font "helvectica,$xfont" scale -0.8,-0.4
340 set mxtics 5
341 set ytics font "helvetica,5" scale -0.4
342 set terminal png size $opts{size}
343 set output "$prefix-heat-reads-$fh_reads_page.png"
344 set view map
345 splot "-" using (\$1+1):2:(\$3>0?log10(\$3):0) matrix rowheaders with image
346 END
347 }
348 print $fh_reads "\"$name\"\t@F[2..$#F]\n" if (/^FREADS/);
349
350
351 #---------- Heatmap DEPTH
352 $fh_depth_count++ if (/^FDEPTH/);
353 if ($fh_depth_count > $opts{page}
354 && $nfile-$opts{page}*$fh_depth_page != 1) {
355 if (defined($fh_depth)) {
356 print $fh_depth "\nend\n";
357 close($fh_depth);
358 system("gnuplot", "$prefix-heat-depth-$fh_depth_page.gp") && die;
359 undef $fh_depth;
360 }
361 $fh_depth_page++;
362 $fh_depth_count = 0;
363 }
364
365 if (/^VDEPTH/ && !defined($fh_depth)) {
366 $fh_depth_nfile = $fh_depth_page * $opts{page} > $nfile
367 ? $nfile - ($fh_depth_page-1) * $opts{page}
368 : $opts{page};
369 $fh_depth_nfile++ if ($fh_depth_page * $opts{page} == $nfile-1);
370 open($fh_depth, ">", $prefix."-heat-depth-$fh_depth_page.gp") ||
371 die "$prefix-heat-depth-$fh_depth_page.gp";
372 print $fh_depth <<"END";
373 set title "average log10(depth) per amplicon, page $fh_depth_page"
374 unset key
375 set xrange [0:$namp+1]
376 set yrange [-1:$fh_depth_nfile]
377 set bmargin at screen 0.07
378 set tmargin at screen 0.91
379 set palette rgbformula 15,4,9
380 set cbrange [0:6]
381 set title font "helvetica,20"
382 set xtics 5 font "helvectica,$xfont" scale -0.8,-0.4
383 set mxtics 5
384 set ytics font "helvetica,5" scale -0.4
385 set terminal png size $opts{size}
386 set output "$prefix-heat-depth-$fh_depth_page.png"
387 set view map
388 splot "-" using (\$1+1):2:(\$3>0?log10(\$3):0) matrix rowheaders with image
389 END
390 }
391 print $fh_depth "\"$name\"\t@F[2..$#F]\n" if (/^VDEPTH/);
392
393
394 #---------- Heatmap AMP mismatch
395 $fh_amp_count++ if (/^FAMP\t\S+\t0/);
396 if ($fh_amp_count > $opts{page} && $nfile-$opts{page}*$fh_amp_page!=1) {
397 if (defined($fh_amp)) {
398 print $fh_amp "\nend\n";
399 close($fh_amp);
400 system("gnuplot", "$prefix-heat-amp-$fh_amp_page.gp") && die;
401 undef $fh_amp;
402 }
403 $fh_amp_page++;
404 $fh_amp_count = 0;
405 }
406
407 if (/^FAMP/ && !defined($fh_amp)) {
408 $fh_amp_nfile = $fh_amp_page * $opts{page} > $nfile
409 ? $nfile - ($fh_amp_page-1) * $opts{page}
410 : $opts{page};
411 $fh_amp_nfile++ if ($fh_amp_page * $opts{page} == $nfile-1);
412 open($fh_amp, ">", $prefix."-heat-amp-$fh_amp_page.gp") ||
413 die "$prefix-heat-amp-$fh_amp_page.gp";
414 print $fh_amp <<"END";
415 set title "Percentage of read-pairs with incorrectly positioned primers, page $fh_amp_page"
416 unset key
417 set xrange [0:$namp+1]
418 set yrange [-1:$fh_amp_nfile]
419 set bmargin at screen 0.07
420 set tmargin at screen 0.91
421 set palette rgbformula 30,31,32
422 set cbrange [0:100]
423 set title font "helvetica,20"
424 set xtics 5 font "helvectica,$xfont" scale -0.8,-0.4
425 set mxtics 5
426 set ytics font "helvetica,5" scale -0.4
427 set terminal png size $opts{size}
428 set output "$prefix-heat-amp-$fh_amp_page.png"
429 set view map
430 splot "-" using (\$1+1):2:3 matrix rowheaders with image
431 END
432 }
433 if (/^FAMP/) {
434 if ($F[2] == 0) {
435 # First line will be summary across all amplicons (amp 0)
436 if ($fh_amp_count == 0) {
437 print $fh_amp "\"$name\"";
438 } else {
439 print $fh_amp "\n\"$name\"";
440 }
441 } else {
442 # Subsequent lines are per amplicon stats,
443 # corrected for small sample size via amp_add.
444 print $fh_amp "\t",
445 100*($F[4]+$F[5])/($F[3]+$F[4]+$F[5]+$opts{amp_add});
446 }
447 }
448
449 #---------- Heatmap READ-PERC, clipped and log
450 $fh_rperc_count++ if (/^FRPERC/);
451 if ($fh_rperc_count > $opts{page}
452 && $nfile-$opts{page}*$fh_rperc_page != 1) {
453 if (defined($fh_rperc)) {
454 print $fh_rperc "\nend\n";
455 close($fh_rperc);
456 system("gnuplot", "$prefix-heat-read-perc-$fh_rperc_page.gp") && die;
457 undef $fh_rperc;
458 }
459 if (defined($fh_rperl)) {
460 print $fh_rperl "\nend\n";
461 close($fh_rperl);
462 system("gnuplot", "$prefix-heat-read-perc-log-$fh_rperc_page.gp") && die;
463 undef $fh_rperl;
464 }
465 $fh_rperc_page++;
466 $fh_rperc_count = 0;
467 }
468
469 if (/^FRPERC/ && !defined($fh_rperc)) {
470 $fh_rperc_nfile = $fh_rperc_page * $opts{page} > $nfile
471 ? $nfile - ($fh_rperc_page-1) * $opts{page}
472 : $opts{page};
473 $fh_rperc_nfile++ if ($fh_rperc_page * $opts{page} == $nfile-1);
474
475 open($fh_rperc, ">", $prefix."-heat-read-perc-$fh_rperc_page.gp") ||
476 die "$prefix-heat-read-perc-$fh_rperc_page.gp";
477 print $fh_rperc <<"END";
478 set title "percentage of reads per amplicon (max 5%), page $fh_rperc_page
479 unset key
480 set xrange [0:$namp+1]
481 set yrange [-1:$fh_rperc_nfile]
482 set bmargin at screen 0.07
483 set tmargin at screen 0.91
484 #set palette rgbformula 6,5,13
485 set palette rgbformula 30,31,32
486 set cbrange [0:5]
487 set title font "helvetica,20"
488 set xtics 5 font "helvectica,$xfont" scale -0.8,-0.4
489 set mxtics 5
490 set ytics font "helvetica,5" scale -0.4
491 set terminal png size $opts{size}
492 set output "$prefix-heat-read-perc-$fh_rperc_page.png"
493 set view map
494 splot "-" using (\$1+1):2:(\$3<5?\$3:5) matrix rowheaders with image
495 END
496
497 open($fh_rperl, ">", $prefix."-heat-read-perc-log-$fh_rperc_page.gp") ||
498 die "$prefix-heat-read-perc-log-$fh_rperc_page.gp";
499 print $fh_rperl <<"END";
500 set title "percentage of reads per amplicon (log10 scale), page $fh_rperc_page
501 unset key
502 set xrange [0:$namp+1]
503 set yrange [-1:$fh_rperc_nfile]
504 set bmargin at screen 0.07
505 set tmargin at screen 0.91
506 #set palette rgbformula 6,5,13
507 set palette rgbformula 30,31,32
508 set cbrange [-1:2]
509 set title font "helvetica,20"
510 set xtics 5 font "helvectica,$xfont" scale -0.8,-0.4
511 set mxtics 5
512 set ytics font "helvetica,5" scale -0.4
513 set terminal png size $opts{size}
514 set output "$prefix-heat-read-perc-log-$fh_rperc_page.png"
515 set view map
516 splot "-" using (\$1+1):2:(\$3>0?log10(\$3):0) matrix rowheaders with image
517 END
518 }
519 print $fh_rperc "\"$name\"\t@F[2..$#F]\n" if (/^FRPERC/);
520 print $fh_rperl "\"$name\"\t@F[2..$#F]\n" if (/^FRPERC/);
521
522 #---------- Heatmap COVERAGE at multiple depths
523 $fh_cover_count{$1}++ if (/^FPCOV-(\d+)/);
524 if ($fh_cover_count{$1} > $opts{page}
525 && $nfile-$opts{page}*$fh_cover_page{$1} != 1) {
526 if (defined($fh_cover{$1})) {
527 my $tmp = $fh_cover{$1};
528 print $tmp "\nend\n";
529 close($fh_cover{$1});
530 system("gnuplot", "$prefix-heat-coverage-$1-$fh_cover_page{$1}.gp") && die;
531 undef $fh_cover{$1};
532 }
533 $fh_cover_page{$1}++;
534 $fh_cover_count{$1} = 0;
535 }
536
537 if (/^FPCOV-(\d+)\t/) {
538 $fh_cover_page{$1}=1 if !defined($fh_cover_page{$1});
539 $fh_cover_nfile{$1} = $fh_cover_page{$1} * $opts{page} > $nfile
540 ? $nfile - ($fh_cover_page{$1}-1) * $opts{page}
541 : $opts{page};
542 $fh_cover_nfile{$1}++ if ($fh_cover_page{$1} * $opts{page}==$nfile-1);
543 if (!defined($fh_cover{$1})) {
544 open($fh_cover{$1}, ">", $prefix."-heat-coverage-$1-$fh_cover_page{$1}.gp") ||
545 die "$prefix-heat-coverage-$1-$fh_cover_page{$1}.gp";
546 my $tmp = $fh_cover{$1};
547 print $tmp <<"END";
548 set title "percentage of amplicon covered to depth $1, page $fh_cover_page{$1}"
549 unset key
550 set xrange [0:$namp+1]
551 set yrange [-1:$fh_cover_nfile{$1}]
552 set bmargin at screen 0.07
553 set tmargin at screen 0.91
554 set palette rgbformula -13,6,-15
555 set cbrange [0:100]
556 set title font "helvetica,20"
557 set xtics 5 font "helvectica,$xfont" scale -0.8, -0.4
558 set mxtics 5
559 set ytics 5 font "helvetica,5" scale -0.4
560 #set grid xtics ytics
561 set terminal png size $opts{size}
562 set output "$prefix-heat-coverage-$1-$fh_cover_page{$1}.png"
563 set view map
564 splot "-" using (\$1+1):2:3 matrix rowheaders with image
565 END
566 }
567 my $tmp = $fh_cover{$1};
568 print $tmp "\"$name\"\t@F[2..$#F]\n";
569 }
570
571 # Graphs with merged file data (mean and SD) for all amplicons
572 local $"="\n";
573 print $ch_reads "\$mean << EOD\n@F[2..$#F]\nEOD\n\n" if (/^CDEPTH.*MEAN/);
574 print $ch_reads "\$sd << EOD\n@F[2..$#F]\nEOD\n\n" if (/^CDEPTH.*STDDEV/);
575 print $ch_depth "\$mean << EOD\n@F[2..$#F]\nEOD\n\n" if (/^CDEPTH.*MEAN/);
576 print $ch_depth "\$sd << EOD\n@F[2..$#F]\nEOD\n\n" if (/^CDEPTH.*STDDEV/);
577 print $ch_rperc "@F[2..$#F]\nend\n" if (/^CRPERC.*(MEAN|STDDEV)/);
578 print $ch_amp 100*($F[4]+$F[5])/($F[3]+$F[4]+$F[5]+$opts{amp_add}),"\n"
579 if (/^CAMP\tCOMBINED\t[1-9]/);
580
581 # These only occur once so we can open the file in situ rather
582 # than keep appending to it.
583 if (/^CPCOV-(\d+).*(MEAN|STDDEV)/) {
584 # ASSUMPTION mean first, followed by stddev.
585 # So open file for MEAN. Close and plot on STDDEV
586 if ($2 eq "MEAN") {
587 open($fh, ">", $prefix."-combined-coverage-$1.gp") ||
588 die "$prefix-combined-coverage-$1.gp";
589 if ($opts{orient} eq "h") {
590 print $fh <<"END";
591 set title "percentage of amplicon covered to depth >= $1, all files"
592 set xlabel "amplicon"
593 set xrange [0:$namp+1]
594 set ylabel "percent covered"
595 set yrange [0:100]
596 set key below
597 set title font "helvetica,20"
598 set xtics 5 font "helvectica,$xfont" scale -2,-1
599 set mxtics 5
600 set terminal pngcairo size $opts{size2}
601 set output "$prefix-combined-coverage-$1.png"
602 plot "-" using (column(0)+1):1 with impulses lw $impw1 lt 1 title "mean", \\
603 "-" using (column(0)+1):1 with impulses lw $impw2 lt 3 title "s.d."
604 END
605 } else {
606 print $fh <<"END";
607 set title "%cover >=$1 deep, all files"
608 set ylabel "amplicon"
609 set yrange [0:$namp+1]
610 set xlabel "percent covered"
611 set xrange [0:100]
612 set grid
613 set key below
614 set title font "helvetica,20"
615 set ytics 5 font "helvectica,$xfont" scale -2,-1
616 set mytics 5
617 set terminal pngcairo size $opts{size3}
618 set output "$prefix-combined-coverage-$1.png"
619 plot "-" using (0):(column(0)+1):1:(0) with vector nohead lw $impw1 lt 1 title "mean", \\
620 "-" using (0):(column(0)+1):1:(0) with vector nohead lw $impw2 lt 3 title "s.d."
621 END
622 }
623 }
624 print $fh "@F[2..$#F]\nend\n";
625
626 if ($2 eq "STDDEV") {
627 close($fh);
628 system("gnuplot", "$prefix-combined-coverage-$1.gp") && die;
629 }
630 }
631
632 if (/^(FREADS|FDEPTH|FVDEPTH)/) {
633 my $max = max(@F[2..$#F]);
634 $max_depth = $max if $max_depth < $max;
635 }
636
637 # Per file graphs; accumulate multiple stats here and dump out at end
638 if (/^(FREADS|FDEPTH|FVDEPTH|FPCOV|FRPERC)/) {
639 $file{$F[1]}{$F[0]} = \@F;
640 #push(@{$file{$F[1]}{keys}}, $F[0]);
641 }
642 if (/^FAMP\s\S+\s[1-9]*/) {
643 push(@{$file{$F[1]}{$F[0]}}, 100*($F[4]+$F[5])/($F[3]+$F[4]+$F[5]+$opts{amp_add}));
644 }
645 if (/^FTCOORD/) {
646 $_ = "";
647 foreach my $x (@F[3..$#F]) {
648 my @a = split(",", $x);
649 $a[0] += $ref_start{$amp2ref[$F[2]]};
650 $a[1] += $ref_start{$amp2ref[$F[2]]};
651 $_ .= join("\t", @a) . "\t$F[2]\n";
652 }
653 s/\n$//;
654 push(@{$file{$F[1]}{$F[0]}}, "$_") if ($_ ne "");
655 }
656
657 if (/^CTCOORD/) {
658 $_ = "";
659 foreach my $x (@F[3..$#F]) {
660 my @a = split(",", $x);
661 $a[0] += $ref_start{$amp2ref[$F[2]]};
662 $a[1] += $ref_start{$amp2ref[$F[2]]};
663 $_ .= join("\t", @a) . "\t$F[2]\n";
664 }
665 s/\n$//;
666 push(@combined_coord, "$_") if ($_ ne "");
667 }
668
669 if(/^[FC]DP_(ALL|VALID)/) {
670 local $"="\n";
671 push(@{$file{$F[1]}{$F[0]}}, "@F[3..$#F]") if ($_ ne "");
672 }
673 }
674
675 #-----------------------------------------------------------------------------
676 # Close gnuplot files and generate PNGs
677
678 #--- Per file
679 if (defined($fh_depth)) {
680 print $fh_depth "\nend\n";
681 close($fh_depth);
682 system("gnuplot", "$prefix-heat-depth-$fh_depth_page.gp") && die;
683 }
684
685 if (defined($fh_amp)) {
686 print $fh_amp "\nend\n";
687 close($fh_amp);
688 system("gnuplot", "$prefix-heat-amp-$fh_amp_page.gp") && die;
689 }
690
691 if (defined($fh_reads)) {
692 print $fh_reads "\nend\n";
693 close($fh_reads);
694 system("gnuplot", "$prefix-heat-reads-$fh_reads_page.gp") && die;
695 }
696
697 if (defined($fh_rperc)) {
698 print $fh_rperc "\nend\n";
699 close($fh_rperc);
700 system("gnuplot", "$prefix-heat-read-perc-$fh_rperc_page.gp") && die;
701 }
702
703 if (defined($fh_rperl)) {
704 print $fh_rperl "\nend\n";
705 close($fh_rperl);
706 system("gnuplot", "$prefix-heat-read-perc-log-$fh_rperc_page.gp") && die;
707 }
708
709 foreach (keys %fh_cover) {
710 if (defined($fh_cover{$_})) {
711 close($fh_cover{$_});
712 system("gnuplot", "$prefix-heat-coverage-$_-$fh_cover_page{$_}.gp") && die;
713 }
714 }
715
716 #--- Combined
717 if ($opts{depth_max}) {
718 if ($max_depth > $opts{depth_max}) {
719 print STDERR "Warning: specified -depth_max is lower than the data maximum of $max_depth\n";
720 }
721 $max_depth = $opts{depth_max};
722 }
723 $max_depth = 10**ceil(log($max_depth+1)/log(10)); # round up to power of 10
724
725 # combined-reads
726 if ($opts{orient} eq "h") {
727 print $ch_reads <<"END";
728 set title "average number of reads per amplicon, all files"
729 unset key
730 set xlabel "amplicon"
731 set xrange [0:$namp+1]
732 set ylabel "no. reads"
733 set yrange [1:$max_depth]
734 set logscale y
735 set key below
736 set title font "helvetica,20"
737 set xtics 5 font "helvetica,$xfont" scale -2,-1
738 set mxtics 5
739 set terminal png size $opts{size2}
740 set output "$prefix-combined-reads.png"
741 plot \$mean using (column(0)+1):(\$1 > 0.001 ? \$1 : 0.001) with impulses lw $impw1 title "mean", \\
742 \$sd using (column(0)+1):(\$1 > 0.001 ? \$1 : 0.001) with impulses lw $impw2 lt 3 title "s.d."
743 END
744 } else {
745 print $ch_reads <<"END";
746 # Compute max X value
747 stats \$mean nooutput
748 max_range = STATS_max
749 stats \$sd nooutput
750 max_range = max_range > STATS_max ? max_range : STATS_max
751 max_range = 10**ceil(log10(max_range+0.01))
752
753 set title "no. of reads, all files"
754 unset key
755 set ylabel "amplicon"
756 set yrange [0:$namp+1]
757 set xlabel "no. reads"
758 set xrange [0:max_range]
759 set grid; # xtics noytics
760 set logscale x
761 set format x "%.g"
762 set xtics font "helvectica,$yfont"
763 set key below
764 set title font "helvetica,20"
765 set ytics 5 font "helvectica,$xfont" scale -2,-1
766 set mytics 5
767 set terminal png size $opts{size3}
768 set output "$prefix-combined-reads.png"
769 plot "\$mean" using (0):(column(0)+1):(\$1 > 0.001 ? (\$1 < max_range ? \$1 : max_range ): 0.001):(0) with vector nohead lw $impw1 title "mean", \\
770 "\$sd" using (0):(column(0)+1):(\$1 > 0.001 ? (\$1 < max_range ? \$1 : max_range ): 0.001):(0) with vector nohead lw $impw2 lt 3 title "s.d."
771 END
772 }
773 close($ch_reads);
774 system("gnuplot", "$prefix-combined-reads.gp") && die;
775
776 # combind-depth
777 if ($opts{orient} eq "h") {
778 print $ch_depth <<"END";
779 set title "average depth per amplicon, all files"
780 set xlabel "amplicon"
781 set xrange [:$namp+1]
782 set ylabel "depth"
783 set logscale y
784 set key below
785 set title font "helvetica,20"
786 set xtics 5 font "helvetica,$xfont" scale -2,-1
787 set mxtics 5
788 set terminal png size $opts{size2}
789 set output "$prefix-combined-depth.png"
790 plot "\$mean" using (column(0)+1):(\$1 > 0.001 ? \$1 : 0.001) with impulses lw $impw1 title "mean", \\
791 "\$sd" using (column(0)+1):(\$1 > 0.001 ? \$1 : 0.001) with impulses lw $impw2 lt 3 title "s.d."
792 END
793 } else {
794 print $ch_depth <<"END";
795 # Compute max X value
796 stats \$mean nooutput
797 max_range = STATS_max
798 stats \$sd nooutput
799 max_range = max_range > STATS_max ? max_range : STATS_max
800 max_range = 10**ceil(log10(max_range+0.01))
801
802 set title "avg depth / amp, all files"
803 set ylabel "amplicon"
804 set yrange [0:$namp+1]
805 set xlabel "depth"
806 set xrange [0:max_range]
807 set grid; # xtics noytics
808 set logscale x
809 set format x "%.g"
810 set xtics font "helvectica,$yfont"
811 set key below
812 set title font "helvetica,20"
813 set ytics 5 font "helvetica,$xfont" scale -2,-1
814 set mytics 5
815 set terminal png size $opts{size3}
816 set output "$prefix-combined-depth.png"
817 plot "\$mean" using (0):(column(0)+1):(\$1 > 0.001 ? (\$1 < max_range ? \$1 : max_range) : 0.001):(0) with vector nohead lw $impw1 title "mean", \\
818 "\$sd" using (0):(column(0)+1):(\$1 > 0.001 ? (\$1 < max_range ? \$1 : max_range) : 0.001):(0) with vector nohead lw $impw2 lt 3 title "s.d."
819 END
820 }
821 close($ch_depth);
822 system("gnuplot", "$prefix-combined-depth.gp") && die;
823
824 close($ch_rperc);
825 system("gnuplot", "$prefix-combined-read-perc.gp") && die;
826
827 close($ch_amp);
828 system("gnuplot", "$prefix-combined-amp.gp") && die;
829
830
831
832 #-----------------------------------------------------------------------------
833 # Also produce per file graphs
834 my $combined_done = 0;
835 foreach my $f (sort keys %file) {
836 next if $f eq "COMBINED";
837 my $ff = $f;
838 $ff =~ s/[\/\\;#\$\{\}]/./g; # safe filename component
839 my $fg = $f; # printable in gnuplot
840 $fg =~ s/_/\\\\_/g;
841
842 #--- Template coordinates vs frequence: FILE
843 goto skip_coord if !exists($file{$f}{FTCOORD});
844 my $fn = "$prefix-$ff-tcoord"; # filename prefix
845 open(my $fh, ">", "$fn.gp") || die "$fn.gp";
846
847 print $fh <<"END";
848 set title "$fg: Template coordinate frequencies"
849 unset key
850 set xlabel "position"
851 set xrange [0:$total_len]
852 set ylabel "frequency"
853 set yrange [*:*]
854 set logscale y
855 set mytics 10
856 set title font "helvetica,20"
857 set xtics out nomirror
858 set mxtics 5
859 set x2tics font "helvectica,6" offset 0,-0.6 scale -0.4 centre nomirror
860
861 set terminal png size $opts{size} truecolor
862 set output "$fn.png"
863 set linetype 1 lc "blue"
864 set linetype 2 lc "red"
865 set linetype 3 lc "black
866 seed=rand(-1)
867 END
868
869 my @x2tics = ();
870 for (my $i=1;$i<=$namp;$i++) {
871 my $col = ($i%2)?"blue":"green";
872 print $fh "set obj rect from $amp_start[$i], graph 0 to $amp_end[$i], graph 1 fillcolor rgb '$col' fillstyle transparent solid 0.1 noborder\n";
873 if ($i%2 == 0) {
874 my $ts = $i>=2 ? $amp_end[$i-1] : $amp_start[$i];
875 my $te = $i<$namp ? $amp_start[$i+1] : $amp_end[$i];
876 my $tpos = ($ts+$te)/2;
877 push(@x2tics, "\"$i\" " . ($amp_start[$i]+$amp_end[$i])/2);
878 }
879 }
880 print $fh "set x2tics (" . join(", ", @x2tics) . ")\n";
881
882 print $fh <<"END";
883 plot "-" using 1:(\$3+rand(0)):(\$2-\$1):(0):(int(\$4)?\$4+1:(int(\$5) % 2)) with vector nohead lw 3 lc var
884 END
885 local $"="\n";
886 print $fh "@{$file{$f}{FTCOORD}}\nend\n";
887
888 close($fh);
889 system("gnuplot", "$fn.gp") && die;
890
891 #--- Template coordinates vs size: FILE
892 my $fn = "$prefix-$ff-tsize"; # filename prefix
893 open(my $fh, ">", "$fn.gp") || die "$fn.gp";
894
895 print $fh <<"END";
896 set title "$fg: Template sizes"
897 unset key
898 set xlabel "position"
899 set xrange [0:$total_len]
900 set ylabel "length"
901 set yrange [10:10000]
902 set logscale y
903 set mytics 10
904 set title font "helvetica,20"
905 set xtics out nomirror
906 set mxtics 5
907 set x2tics font "helvectica,6" offset 0,-0.6 scale -0.4 centre nomirror
908
909 set terminal png size $opts{size} truecolor
910 set output "$fn.png"
911 set linetype 1 lc "blue"
912 set linetype 2 lc "red"
913 set linetype 3 lc "black
914 seed=rand(-1)
915 END
916
917 my @x2tics = ();
918 for (my $i=1;$i<=$namp;$i++) {
919 my $col = ($i%2)?"blue":"green";
920 print $fh "set obj rect from $amp_start[$i], graph 0 to $amp_end[$i], graph 1 fillcolor rgb '$col' fillstyle transparent solid 0.1 noborder\n";
921 if ($i%2 == 0) {
922 my $ts = $i>=2 ? $amp_end[$i-1] : $amp_start[$i];
923 my $te = $i<$namp ? $amp_start[$i+1] : $amp_end[$i];
924 my $tpos = ($ts+$te)/2;
925 push(@x2tics, "\"$i\" " . ($amp_start[$i]+$amp_end[$i])/2);
926 }
927 }
928 print $fh "set x2tics (" . join(", ", @x2tics) . ")\n";
929
930 print $fh <<"END";
931 plot "-" using 1:(\$2-\$1+sqrt(\$3)*rand(0)):(\$2-\$1):(0):(int(log(sqrt(\$3)))) with vector nohead lw 2 lc var
932 END
933
934 # Sort by descending freq so high freq colours are on top.
935 local $"="\n";
936 my @sorted = sort {
937 [split(/\s+/,$a)]->[2] <=> [split(/\s+/,$b)]->[2]
938 } split("\n", "@{$file{$f}{FTCOORD}}");
939 print $fh "@sorted\nend\n";
940
941 close($fh);
942 system("gnuplot", "$fn.gp") && die;
943 skip_coord:
944
945 #--- Template coordinates: COMBINED
946 if (scalar(@combined_coord) > 0 && !$combined_done) {
947 my $fn = "$prefix-combined-tcoord"; # filename prefix
948 open(my $fh, ">", "$fn.gp") || die "$fn.gp";
949
950 print $fh <<"END";
951 set title "Template coordinate frequencies, all files"
952 unset key
953 set xlabel "position"
954 set xrange [0:$total_len]
955 set ylabel "frequency"
956 set yrange [*:*]
957 set logscale y
958 set mytics 10
959 set title font "helvetica,20"
960 set xtics out nomirror
961 set mxtics 5
962 set x2tics font "helvectica,6" offset 0,-0.6 scale -0.4 centre nomirror
963
964 set terminal png size $opts{size} truecolor
965 set output "$fn.png"
966 set linetype 1 lc "blue"
967 set linetype 2 lc "red"
968 set linetype 3 lc "black
969 seed=rand(-1)
970 END
971
972 my @x2tics = ();
973 for (my $i=1;$i<=$namp;$i++) {
974 my $col = ($i%2)?"blue":"green";
975 print $fh "set obj rect from $amp_start[$i], graph 0 to $amp_end[$i], graph 1 fillcolor rgb '$col' fillstyle transparent solid 0.1 noborder\n";
976 if ($i%2 == 0) {
977 my $ts = $i>=2 ? $amp_end[$i-1] : $amp_start[$i];
978 my $te = $i<$namp ? $amp_start[$i+1] : $amp_end[$i];
979 my $tpos = ($ts+$te)/2;
980 push(@x2tics, "\"$i\" " . ($amp_start[$i]+$amp_end[$i])/2);
981 }
982 }
983 print $fh "set x2tics (" . join(", ", @x2tics) . ")\n";
984
985 print $fh <<"END";
986 plot "-" using 1:(\$3+rand(0)):(\$2-\$1):(0):(int(\$4)?\$4+1:(int(\$5) % 2)) with vector nohead lw 3 lc var
987 END
988 local $"="\n";
989 print $fh "@combined_coord\nend\n";
990
991 close($fh);
992 system("gnuplot", "$fn.gp") && die;
993 }
994
995
996 #--- Template depth: FILE
997 my $fn = "$prefix-$ff-tdepth"; # filename prefix
998 open(my $fh, ">", "$fn.gp") || die "$fn.gp";
999
1000 print $fh <<"END";
1001 set title "$fg: Template depth per base"
1002 set key below
1003 set xlabel "position"
1004 set xrange [0:$total_len]
1005 set ylabel "depth"
1006 set yrange [1:$max_depth]
1007 set logscale y
1008 set mytics 10
1009 set title font "helvetica,20"
1010 set xtics out nomirror
1011 set mxtics 5
1012 set x2tics font "helvectica,6" offset 0,-0.6 scale -0.4 centre nomirror
1013
1014 set terminal png size $opts{size2} truecolor
1015 set output "$fn.png"
1016 set linetype 1 lc "blue"
1017 set linetype 2 lc "#00B000"
1018 xa=0
1019 xv=0
1020 END
1021
1022 my @x2tics = ();
1023 for (my $i=1;$i<=$namp;$i++) {
1024 my $col = ($i%2)?"blue":"green";
1025 print $fh "set obj rect from $amp_start[$i], graph 0 to $amp_end[$i], graph 1 fillcolor rgb '$col' fillstyle transparent solid 0.1 noborder\n";
1026 if ($i%2 == 0) {
1027 my $ts = $i>=2 ? $amp_end[$i-1] : $amp_start[$i];
1028 my $te = $i<$namp ? $amp_start[$i+1] : $amp_end[$i];
1029 my $tpos = ($ts+$te)/2;
1030 push(@x2tics, "\"$i\" " . ($amp_start[$i]+$amp_end[$i])/2);
1031 }
1032 }
1033 print $fh "set x2tics (" . join(", ", @x2tics) . ")\n";
1034
1035 print $fh <<"END";
1036 plot "-" using (xa=xa+\$2):(\$1+.1) with fsteps lw 1 title "all templates", \\
1037 "-" using (xv=xv+\$2):(\$1+.1) with fsteps lw 2 title "valid templates"
1038 END
1039 if (exists($file{$f}{FDP_ALL})) {
1040 foreach (split(/\s+/,"@{$file{$f}{FDP_ALL}}")) {
1041 s/,/ /;
1042 print $fh "$_\n";
1043 }
1044 }
1045 print $fh "end\n";
1046
1047 if (exists($file{$f}{FDP_VALID})) {
1048 foreach (split(/\s+/, "@{$file{$f}{FDP_VALID}}")) {
1049 s/,/ /;
1050 print $fh "$_\n";
1051 }
1052 }
1053 print $fh "end\n";
1054
1055 close($fh);
1056 system("gnuplot", "$fn.gp") && die;
1057
1058
1059 #--- Template depth: FILE
1060 if (!$combined_done) {
1061 my $fn = "$prefix-combined-tdepth"; # filename prefix
1062 open(my $fh, ">", "$fn.gp") || die "$fn.gp";
1063
1064 print $fh <<"END";
1065 set title "Template depth per base, all files"
1066 set key below
1067 set xlabel "position"
1068 set xrange [0:$total_len]
1069 set ylabel "depth"
1070 set yrange [1:$max_depth]
1071 set logscale y
1072 set mytics 10
1073 set title font "helvetica,20"
1074 set xtics out nomirror
1075 set mxtics 5
1076 set x2tics font "helvectica,6" offset 0,-0.6 scale -0.4 centre nomirror
1077
1078 set terminal png size $opts{size2} truecolor
1079 set output "$fn.png"
1080 set linetype 1 lc "blue"
1081 set linetype 2 lc "#00B000"
1082 xa=0
1083 xv=0
1084 END
1085
1086 my @x2tics = ();
1087 for (my $i=1;$i<=$namp;$i++) {
1088 my $col = ($i%2)?"blue":"green";
1089 print $fh "set obj rect from $amp_start[$i], graph 0 to $amp_end[$i], graph 1 fillcolor rgb '$col' fillstyle transparent solid 0.1 noborder\n";
1090 if ($i%2 == 0) {
1091 my $ts = $i>=2 ? $amp_end[$i-1] : $amp_start[$i];
1092 my $te = $i<$namp ? $amp_start[$i+1] : $amp_end[$i];
1093 my $tpos = ($ts+$te)/2;
1094 push(@x2tics, "\"$i\" " . ($amp_start[$i]+$amp_end[$i])/2);
1095 }
1096 }
1097 print $fh "set x2tics (" . join(", ", @x2tics) . ")\n";
1098
1099 print $fh <<"END";
1100 plot "-" using (xa=xa+\$2):((\$1+.1)/$nfile) with fsteps lw 1 title "all templates", \\
1101 "-" using (xv=xv+\$2):((\$1+.1)/$nfile) with fsteps lw 2 title "valid templates"
1102 END
1103 if (exists($file{COMBINED}{CDP_ALL})) {
1104 foreach (split(/\s+/,"@{$file{COMBINED}{CDP_ALL}}")) {
1105 s/,/ /;
1106 print $fh "$_\n";
1107 }
1108 }
1109 print $fh "end\n";
1110
1111 if (exists($file{COMBINED}{CDP_VALID})) {
1112 foreach (split(/\s+/, "@{$file{COMBINED}{CDP_VALID}}")) {
1113 s/,/ /;
1114 print $fh "$_\n";
1115 }
1116 }
1117 print $fh "end\n";
1118
1119 close($fh);
1120 system("gnuplot", "$fn.gp") && die;
1121 }
1122
1123 #--- Read count / depth
1124 my $fn = "$prefix-$ff-reads"; # filename prefix
1125 open(my $fh, ">", "$fn.gp") || die "$fn.gp";
1126
1127 if ($opts{orient} eq "h") {
1128 print $fh <<"END";
1129 set title "$fg: read count per amplicon
1130 set key below
1131 set xlabel "amplicon"
1132 set xrange [0:$namp+1]
1133 set ylabel "no. reads"
1134 set logscale y
1135 set yrange [1:$max_depth]
1136 set title font "helvetica,20"
1137 set xtics 5 font "helvectica,$xfont" scale -2,-1
1138 set mxtics 5
1139 set terminal png size $opts{size2}
1140 set output "$fn.png"
1141 plot "-" using (column(0)+1):(\$1 > 0.001 ? \$1 : 0.001) with impulses lw $impw1 title "#reads", \\
1142 "-" using (column(0)+1):(\$1 > 0.001 ? \$1 : 0.001) with impulses lw $impw2 lt 5 title "all depth",\\
1143 "-" using (column(0)+1):(\$1 > 0.001 ? \$1 : 0.001) with impulses lw $impw2 lt 3 title "usable depth",
1144 END
1145 } else {
1146 print $fh <<"END";
1147 set title "$fg:\\nread count per amplicon
1148 set key below
1149 set ylabel "amplicon"
1150 set yrange [0:$namp+1]
1151 set xlabel "no. reads"
1152 set xrange [1:$max_depth]
1153 set grid; # xtics noytics
1154 set logscale x
1155 set xtics font "helvectica,$yfont"
1156 set title font "helvetica,20"
1157 set ytics 5 font "helvectica,$xfont" scale -2,-1
1158 set mytics 5
1159 set terminal png size $opts{size3}
1160 set output "$fn.png"
1161 plot "-" using (0):(column(0)+1):(\$1 > 0.001 ? (\$1 < $max_depth ? \$1 : $max_depth) : 0.001):(0) with vector nohead lw $impw1 title "#reads", \\
1162 "-" using (0):(column(0)+1):(\$1 > 0.001 ? (\$1 < $max_depth ? \$1 : $max_depth) : 0.001):(0) with vector nohead lw $impw2 lt 5 title "all depth",\\
1163 "-" using (0):(column(0)+1):(\$1 > 0.001 ? (\$1 < $max_depth ? \$1 : $max_depth) : 0.001):(0) with vector nohead lw $impw2 lt 3 title "usable depth",
1164 END
1165 }
1166
1167 local $"="\n";
1168 print $fh "@{$file{$f}{FREADS}}[2..$#{$file{$f}{FREADS}}]\nend\n";
1169 print $fh "@{$file{$f}{FDEPTH}}[2..$#{$file{$f}{FDEPTH}}]\nend\n";
1170 print $fh "@{$file{$f}{FVDEPTH}}[2..$#{$file{$f}{FVDEPTH}}]\nend\n";
1171
1172 close($fh);
1173 system("gnuplot", "$fn.gp") && die;
1174
1175 #--- Read %coverage and %read-per-amplicon
1176 my $fn = "$prefix-$ff-cov"; # filename prefix
1177 open(my $fh, ">", "$fn.gp") || die "$fn.gp";
1178
1179 if ($opts{orient} eq "h") {
1180 print $fh <<"END";
1181 set title "$fg: percentage coverage to specific depth(s)"
1182 set key below
1183 set xlabel "amplicon"
1184 set xrange [0:$namp+1]
1185 set ylabel "%coverage"
1186 set yrange [0:100]
1187 set title font "helvetica,20"
1188 set xtics 5 font "helvectica,$xfont" scale -2,-1
1189 set mxtics 5
1190 set ytics nomirror
1191 set y2tics
1192
1193 set terminal png size $opts{size2}
1194 set output "$fn.png"
1195 plot \\
1196 END
1197 } else {
1198 print $fh <<"END";
1199 set title "$fg: % coverage at depth(s)"
1200 set key below
1201 set ylabel "amplicon"
1202 set yrange [$namp+1:0]
1203 #set xlabel "%coverage"
1204 set xrange [0:100]
1205 set title font "helvetica,14"
1206 set ytics 5 font "helvectica,$xfont" scale -2,-1
1207 set mytics 5
1208 set xtics 10 font "helvetica,12"
1209 set mxtics 5
1210 set x2tics 10 font "helvetica,12"
1211
1212 set terminal png size $opts{size3}
1213 set output "$fn.png"
1214 plot \\
1215 END
1216 }
1217 #my $lw = 5;
1218 my $lw = ($namp >= 100) ? 3 : 2;
1219 my @lt = ("rgb \"#900020\"","rgb \"#2060FF\"",2,3,4,5,6,7,8,9);
1220 my $lt = 0;
1221 foreach (sort {
1222 my $x=$a; $x=~s/FPCOV-(\d+)/\1/;
1223 my $y=$b; $y=~s/FPCOV-(\d+)/\1/;
1224 $x <=> $y
1225 } keys %{$file{$f}}) {
1226 next unless /^FPCOV-(\d+)/;
1227 if ($opts{orient} eq "h") {
1228 print $fh "\"-\" using (column(0)+1):1 with impulses lw $lw lt $lt[$lt] title \"\depth >= $1\", \\\n";
1229 $lw += ($namp >= 100) ? ($lw<4) : ($lw<5)*2;
1230 #print $fh "\"-\" using (column(0)+1):1 with linespoints lw $lw lt $lt[$lt] title \"\%coverage >= $1\", \\\n";
1231 #$lw -= ($lw>1)*2;
1232 } else {
1233 print $fh "\"-\" using (0):(column(0)+1):1:(0) with vector nohead lw $lw lt $lt[$lt] title \"depth>=$1\", \\\n";
1234 $lw += ($lw<5)*2;
1235 }
1236 $lt++;
1237 }
1238 print $fh "\n";
1239 #print $fh "\"-\" with linespoints lw 3 lt 3 title \"\%reads/amp\" axis x1y2\n";
1240
1241 #plot "-" with linespoints lw 3 title "%coverage", \\
1242 # "-" with linespoints lw 3 lt 3 title "%reads/amp" axis x1y2
1243 #END
1244
1245 local $"="\n";
1246 foreach (sort {
1247 my $x=$a; $x=~s/FPCOV-(\d+)/\1/;
1248 my $y=$b; $y=~s/FPCOV-(\d+)/\1/;
1249 $x <=> $y
1250 } keys %{$file{$f}}) {
1251 next unless /^FPCOV-(\d+)/;
1252 print $fh "@{$file{$f}{$_}}[2..$#{$file{$f}{$_}}]\nend\n";
1253 }
1254
1255 close($fh);
1256 system("gnuplot", "$fn.gp") && die;
1257
1258
1259 #--- Amplicon primer correctness
1260 my $fn = "$prefix-$ff-amp"; # filename prefix
1261 open(my $fh, ">", "$fn.gp") || die "$fn.gp";
1262
1263 if ($opts{orient} eq "h") {
1264 print $fh <<"END";
1265 set title "$fg: Percentage of read-pairs with incorrectly positioned primers"
1266 unset key
1267 set xlabel "amplicon"
1268 set xrange [0:$namp+1]
1269 set ylabel "%incorrect"
1270 set yrange [0:100]
1271 set title font "helvetica,20"
1272 set xtics 5 font "helvectica,$xfont" scale -2,-1
1273 set mxtics 5
1274
1275 set terminal png size $opts{size2}
1276 set output "$fn.png"
1277 plot "-" using (column(0)+1):1 with impulses lw $impw1
1278 END
1279 } else {
1280 print $fh <<"END";
1281 set title "$fg:\\n% mis-priming read-pairs"
1282 unset key
1283 set grid
1284 set ylabel "amplicon"
1285 set yrange [0:$namp+1]
1286 set xlabel "%incorrect"
1287 set xrange [0:100]
1288 set title font "helvetica,20"
1289 set ytics 5 font "helvectica,$xfont" scale -2,-1
1290 set mytics 5
1291 set terminal png size $opts{size3}
1292 set output "$fn.png"
1293 plot "-" using (0):(column(0)+1):1:(0) with vector nohead lw $impw1
1294 END
1295 }
1296 local $"="\n";
1297 print $fh "@{$file{$f}{FAMP}}[1..$#{$file{$f}{FAMP}}]\nend\n";
1298
1299 close($fh);
1300 system("gnuplot", "$fn.gp") && die;
1301
1302 $combined_done = 1;
1303 }
1304
1305 #---------------------------------
1306 #adding html file to show plots
1307 index_only:
1308
1309 # index.html is in the same dir as the images
1310 my ($prefix_dir) = $prefix=~/(.*\/)/;
1311 my $fname = "${prefix_dir}index.html";
1312
1313 open(my $fh,'>',$fname) or error("$fname: $!");
1314 print $fh q[
1315 <html>
1316 <head>
1317 <style>
1318 .thumbnail
1319 {
1320 text-decoration:none;
1321 color:black;
1322 font-weight:bold;
1323 }
1324 .imgs td
1325 {
1326 vertical-align:middle;
1327 padding: 0.5em;
1328 border: 1px solid black;
1329 }
1330 table.imgs
1331 {
1332 border-collapse:collapse;
1333 margin-left:20px;
1334 }
1335 </style>
1336 </head>
1337 <body>
1338 <table class="imgs">
1339 ];
1340
1341 my @imgs = sort {
1342 my ($A,$B)=($a,$b);
1343 $A=~s/(\d+)/sprintf("%09d",$1)/ge;
1344 $B=~s/(\d+)/sprintf("%09d",$1)/ge;
1345 $A cmp $B
1346 } grep {!/thumb\.png/} <"$prefix*.png">;
1347
1348 my $last = "";
1349 my $new_section = 0;
1350 for (my ($i,$j)=(0,0); $i < @imgs; $i++, $j++)
1351 {
1352 my $fn_base = $imgs[$i];
1353 $fn_base =~ s/-(\w+|read-perc|read-perc-log)(-\d+)*\.png$//;
1354 if ($fn_base ne $last || $j % 6 == 0) {
1355 if ($fn_base ne $last) {
1356 $last=$fn_base;
1357 $new_section = 1;
1358 $j = 0;
1359 }
1360
1361 if ( $i>0 ) { print $fh "</tr>\n"; }
1362 if ($new_section) {
1363 print $fh "<tr><td>$fn_base</td></tr>\n";
1364 $new_section = 0;
1365 }
1366 print $fh "<tr>";
1367 }
1368 my ($img) = $imgs[$i]=~/(?:.*\/)?(.*)/;
1369 if ($opts{thumbnails}) {
1370 my $scale = 100*$opts{thumb_size}/[split(",",$opts{size})]->[0],"\n";
1371 system("convert", "-scale", "$scale%", $imgs[$i], "$imgs[$i].thumb.png") && die;
1372 print $fh qq[<td><a class="thumbnail" href="$img"><img src="$img.thumb.png" width="$opts{thumb_size}px"></a></td>\n];
1373 } else {
1374 print $fh qq[<td><a class="thumbnail" href="$img"><img src="$img" width="$opts{thumb_size}px"></a></td>\n];
1375 }
1376 }
1377 print $fh qq[
1378 </tr>
1379 </table>
1380 </body>
1381 </html>
1382 ];
1383
1384 close($fh);