-
Notifications
You must be signed in to change notification settings - Fork 104
/
bismark2report
executable file
·1300 lines (1081 loc) · 48.3 KB
/
bismark2report
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
use warnings;
use strict;
use Getopt::Long;
use FindBin qw($RealBin);
use lib "$RealBin/../lib";
## This program is Copyright (C) 2010-23, Felix Krueger (fkrueger@altoslabs.com)
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
my $bismark2report_version = 'v0.24.2';
my (@alignment_reports,@dedup_reports,@splitting_reports,@mbias_reports,@nuc_reports);
my ($output_dir,$verbose,$manual_output_file) = process_commandline();
# print join (",",@alignment_reports)."\n";
# print join (",",@dedup_reports)."\n";
# print join (",",@splitting_reports)."\n";
# print join (",",@mbias_reports)."\n";
# print join (",",@nuc_reports)."\n";
while (@alignment_reports){
my $alignment_report = shift @alignment_reports;
my $dedup_report = shift @dedup_reports;
my $splitting_report = shift @splitting_reports;
my $mbias_report = shift @mbias_reports;
my $nuc_report = shift @nuc_reports;
### HTML OUTPUT FILE
my $report_output = $alignment_report;
$report_output =~ s/^.*\///; # deleting optional path information
$report_output =~ s/\.txt$//;
$report_output =~ s/$/.html/;
# if -o output_file was specified we are going to use that name preferentially.
# This may only happen if there is a single report in the folder, or if a single report has been specified manually
if ($manual_output_file){
warn "A specific output filename was specified: $manual_output_file. Using that one instead of deriving the filename\n"; sleep(1);
$report_output = $manual_output_file;
}
$report_output = $output_dir.$report_output;
warn "\nWriting Bismark HTML report to >> $report_output <<\n\n";
# Get the report template into a string
my $doc = read_report_template('plotly_template.tpl');
# Get the plot.ly code into a string. This makes the template so much more managable and also allows for quick replacement of the plot.ly
# code itself should it get updated
my $plotly_code = read_report_template('plot.ly');
# replacing the Plot.ly spaceholders with the actual plot.ly code
if ($doc =~ s/\{\{plotly_goes_here\}\}.*\{\{plotly_goes_here\}\}/$plotly_code/s){ # treating the string as a single line
warn "Plot.ly injection successful!\n" if $verbose;
}
else{
die "Plot.ly incjection not working, won't be able to construct any meaningful HTML reports in this case....\n\n";
}
my $bismark_logo = read_report_template('bismark.logo');
if ($doc =~ s/\{\{bismark_logo_goes_here\}\}.*\{\{bismark_logo_goes_here\}\}/$bismark_logo/s){ # treating the string as a single line
warn "bismark.logo injection successful!\n" if $verbose;
}
my $bioinf_logo = read_report_template('bioinf.logo');
if ($doc =~ s/\{\{bioinf_logo_goes_here\}\}.*\{\{bioinf_logo_goes_here\}\}/$bioinf_logo/s){ # treating the string as a single line
warn "bioinf.logo injection successful!\n" if $verbose;
}
# Create timestamp
$doc = getLoggingTime($doc);
# BISMARK ALIGNMENT REPORT (mandatory)
warn "="x110,"\n";
warn "Using the following alignment report:\t\t> $alignment_report <\n";
$doc = read_alignment_report($alignment_report,$doc);
# DEDUPLICATION REPORT (optional)
if ($dedup_report){
warn "Using the following deduplication report:\t> $dedup_report <\n";
$doc =~ s/\{\{deduplication_section\}\}//g; # deleting these tags and then go ahead and fill the table
$doc = read_deduplication_report($dedup_report,$doc);
}
else{
warn "No deduplication report file specified, skipping this step\n";
# deleting the entire Deduplication Section from the template
$doc =~ s/\{\{deduplication_section\}\}.*\{\{deduplication_section\}\}//s; # treating the string as a single line
}
# SPLITTING REPORT (optional)
if ($splitting_report){
warn "Using the following splitting report:\t\t> $splitting_report <\n";
$doc =~ s/\{\{cytosine_methylation_post_deduplication_section\}\}//g; # deleting these tags and then go ahead and fill the table
$doc = read_splitting_report($splitting_report,$doc);
}
else{
warn "No splitting report file specified, skipping this step\n";
# deleting the entire Splitting Report C context section from the template
$doc =~ s/\{\{cytosine_methylation_post_deduplication_section\}\}.*\{\{cytosine_methylation_post_deduplication_section\}\}//s; # treating the string as a single line
}
# M-BIAS REPORT (optional)
if ($mbias_report){
warn "Using the following M-bias report:\t\t> $mbias_report <\n";
$doc =~ s/\{\{mbias_r1_section\}\}//g; # deleting these tags and then go ahead and fill the table
(my $state, $doc) = read_mbias_report($mbias_report,$doc);
# warn "OK have read the report\n\n";
# warn ">$state<<<\n";
# sleep(1);
if ($state eq 'single'){
# warn "Read was $state: Deleting the M-bias2 section\n";
$doc =~ s/(\{\{mbias_r2_section\}\}.*\{\{mbias_r2_section\}\})//s; # treating the string as a single line
# warn "Deleting: $1\n\n";sleep(5);
}
else{ # paired-end
$doc =~ s/\{\{mbias_r2_section\}\}//g; # deleting these tags and then go ahead and fill the table
}
}
else{
warn "No M-bias report file specified, skipping this step\n";
$doc =~ s/\{\{mbias_r1_section\}\}.*\{\{mbias_r1_section\}\}//s; # treating the string as a single line
$doc =~ s/\{\{mbias_r2_section\}\}.*\{\{mbias_r2_section\}\}//s; # treating the string as a single line
}
# NUCLEOTIDE COVERAGE REPORT (optional)
if ($nuc_report){
warn "Using the following nucleotide coverage report:\t> $nuc_report <\n";
$doc =~ s/\{\{nucleotide_coverage_section\}\}//g; # deleting these tags and then go ahead and fill the table
$doc = read_nucleotide_coverage_report($nuc_report,$doc);
}
else{
warn "No nucleotide coverage report file specified, skipping this step\n";
# deleting the entire Nucleotide Coverage section from the template
# warn "Deleting the entire Nucleotide Coverage section!!!\n\n";
# print $doc;
$doc =~ s/\{\{nucleotide_coverage_section\}\}.*\{\{nucleotide_coverage_section\}\}//s; # treating the string as a single line
}
warn "="x110,"\n\n\n";
$verbose and sleep(3);
write_out_report($report_output,$doc);
}
sub write_out_report{
my ($report_output,$doc) = @_;
open (OUT,'>',$report_output) or die "Failed to write to output file $report_output: $!\n\n";
print OUT $doc;
}
sub getLoggingTime {
my $doc = shift;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
my $time = sprintf ("%02d:%02d:%02d", $hour,$min,$sec);
my $date = sprintf ("%04d-%02d-%02d", $year+1900,$mon+1,$mday);
warn "Using Time: $time, and date: $date\n\n" if ($verbose);
$doc =~ s/\{\{date\}\}/$date/g;
$doc =~ s/\{\{time\}\}/$time/g;
return $doc;
}
sub read_alignment_report{
my ($alignment_report,$doc) = @_;
warn "Processing alignment report $alignment_report ...\n";
open (ALN,$alignment_report) or die "Couldn't read from file $alignment_report: $!\n\n";
my $unique;
my $no_aln;
my $multiple;
my $no_genomic;
my $total_seqs;
my $bismark_version;
my $input_filename;
my $unique_text;
my $no_aln_text;
my $multiple_text;
my $total_seq_text;
my $total_C_count;
my ($meth_CpG,$meth_CHG,$meth_CHH,$meth_unknown);
my ($unmeth_CpG,$unmeth_CHG,$unmeth_CHH,$unmeth_unknown);
my ($perc_CpG,$perc_CHG,$perc_CHH,$perc_unknown);
my $number_OT;
my $number_CTOT;
my $number_CTOB;
my $number_OB;
while (<ALN>){
chomp;
### General Alignment stats
if ($_ =~ /^Sequence pairs analysed in total:/ ){ ## Paired-end
(undef,$total_seqs) = split /\t/;
print "Total paired seqs: >> $total_seqs <<\n" if ($verbose);
$total_seq_text = 'Sequence pairs analysed in total';
}
elsif ($_ =~ /^Sequences analysed in total:/ ){ ## Single-end
(undef,$total_seqs) = split /\t/;
$total_seq_text = 'Sequences analysed in total';
print "total single-end seqs >> $total_seqs <<\n" if ($verbose);
}
elsif($_ =~ /^Bismark report for: (.*) \(version: (.*)\)/){
$input_filename = $1;
$bismark_version = $2;
print "Input filename(s) >> $input_filename <<\n" if ($verbose);
print "Bismark version >> $bismark_version <<\n" if ($verbose);
}
elsif($_ =~ /^Number of paired-end alignments with a unique best hit:/){ ## Paired-end
(undef,$unique) = split /\t/;
print "Unique PE>> $unique <<\n" if ($verbose);;
$unique_text = 'Paired-end alignments with a unique best hit';
}
elsif($_ =~ /^Number of alignments with a unique best hit from/){ ## Single-end
(undef,$unique) = split /\t/;
print "Unique SE>> $unique <<\n" if ($verbose);
$unique_text = 'Single-end alignments with a unique best hit';
}
elsif($_ =~ /^Sequence pairs with no alignments under any condition:/){ ## Paired-end
(undef,$no_aln) = split /\t/;
print "No alignment PE >> $no_aln <<\n" if ($verbose);
$no_aln_text = 'Pairs without alignments under any condition';
}
elsif($_ =~ /^Sequences with no alignments under any condition:/){ ## Single-end
(undef,$no_aln) = split /\t/;
print "No alignments SE>> $no_aln <<\n" if ($verbose);
$no_aln_text = 'Sequences without alignments under any condition';
}
elsif($_ =~ /^Sequence pairs did not map uniquely:/){ ## Paired-end
(undef,$multiple) = split /\t/;
print "Multiple alignments PE >> $multiple <<\n" if ($verbose);
$multiple_text = 'Pairs that did not map uniquely';
}
elsif($_ =~ /^Sequences did not map uniquely:/){ ## Single-end
(undef,$multiple) = split /\t/;
print "Multiple alignments SE >> $multiple <<\n" if ($verbose);
$multiple_text = 'Sequences that did not map uniquely';
}
elsif($_ =~ /^Sequence pairs which were discarded because genomic sequence could not be extracted:/){ ## Paired-end
(undef,$no_genomic) = split /\t/;
print "No genomic sequence PE >> $no_genomic <<\n" if ($verbose);
}
elsif($_ =~ /^Sequences which were discarded because genomic sequence could not be extracted:/){ ## Single-end
(undef,$no_genomic) = split /\t/;
print "No genomic sequence SE>> $no_genomic <<\n" if ($verbose);
}
### Context Methylation
elsif($_ =~ /^Total number of C/ ){
(undef,$total_C_count) = split /\t/;
print "Total number C >> $total_C_count <<\n" if ($verbose);
}
elsif($_ =~ /^Total methylated C\'s in CpG context:/ ){
(undef,$meth_CpG) = split /\t/;
print "meth CpG >> $meth_CpG <<\n" if ($verbose);
}
elsif($_ =~ /^Total methylated C\'s in CHG context:/ ){
(undef,$meth_CHG) = split /\t/;
print "meth CHG >> $meth_CHG <<\n" if ($verbose);
}
elsif($_ =~ /^Total methylated C\'s in CHH context:/ ){
(undef,$meth_CHH) = split /\t/;
print "meth CHH >> $meth_CHH <<\n" if ($verbose);
}
elsif($_ =~ /^Total methylated C\'s in Unknown context:/ ){
(undef,$meth_unknown) = split /\t/;
print "meth Unknown >> $meth_unknown <<\n" if ($verbose);
}
elsif($_ =~ /^Total unmethylated C\'s in CpG context:/ or $_ =~ /^Total C to T conversions in CpG context:/){
(undef,$unmeth_CpG) = split /\t/;
print "unmeth CpG >> $unmeth_CpG <<\n" if ($verbose);
}
elsif($_ =~ /^Total unmethylated C\'s in CHG context:/ or $_ =~ /^Total C to T conversions in CHG context:/){
(undef,$unmeth_CHG) = split /\t/;
print "unmeth CHG >> $unmeth_CHG <<\n" if ($verbose);
}
elsif($_ =~ /^Total unmethylated C\'s in CHH context:/ or $_ =~ /^Total C to T conversions in CHH context:/){
(undef,$unmeth_CHH) = split /\t/;
print "unmeth CHH >> $unmeth_CHH <<\n"if ($verbose);
}
elsif($_ =~ /^Total unmethylated C\'s in Unknown context:/ or $_ =~ /^Total C to T conversions in Unknown context:/){
(undef,$unmeth_unknown) = split /\t/;
print "unmeth Unknown >> $unmeth_unknown <<\n" if ($verbose);
}
elsif($_ =~ /^C methylated in CpG context:/ ){
(undef,$perc_CpG) = split /\t/;
$perc_CpG =~ s/%//;
print "percentage CpG >> $perc_CpG <<\n" if ($verbose);
}
elsif($_ =~ /^C methylated in CHG context:/ ){
(undef,$perc_CHG) = split /\t/;
$perc_CHG =~ s/%//;
print "percentage CHG >> $perc_CHG <<\n" if ($verbose);
}
elsif($_ =~ /^C methylated in CHH context:/ ){
(undef,$perc_CHH) = split /\t/;
$perc_CHH =~ s/%//;
print "percentage CHH >> $perc_CHH <<\n" if ($verbose);
}
elsif($_ =~ /^C methylated in Unknown context \(CN or CHN\):/ ){
(undef,$perc_unknown) = split /\t/;
$perc_unknown =~ s/%//;
print "percentage Unknown >> $perc_unknown <<\n" if ($verbose);
}
### Strand Origin
elsif($_ =~ /^CT\/GA\/CT:/ ){ ## Paired-end
(undef,$number_OT) = split /\t/;
print "Number OT PE>> $number_OT <<\n" if ($verbose);
}
elsif($_ =~ /^CT\/CT:/ ){ ## Single-end
(undef,$number_OT) = split /\t/;
print "Number OT SE>> $number_OT <<\n" if ($verbose);
}
elsif($_ =~ /^GA\/CT\/CT:/ ){ ## Paired-end
(undef,$number_CTOT) = split /\t/;
print "Number CTOT PE >> $number_CTOT <<\n" if ($verbose);
}
elsif($_ =~ /^GA\/CT:/ ){ ## Single-end
(undef,$number_CTOT) = split /\t/;
print "Number CTOT SE >> $number_CTOT <<\n" if ($verbose);
}
elsif($_ =~ /^GA\/CT\/GA:/ ){ ## Paired-end
(undef,$number_CTOB) = split /\t/;
print "Number CTOB PE >> $number_CTOB <<\n" if ($verbose);
}
elsif($_ =~ /^GA\/GA:/ ){ ## Single-end
(undef,$number_CTOB) = split /\t/;
print "Number CTOB SE >> $number_CTOB <<\n" if ($verbose);
}
elsif($_ =~ /^CT\/GA\/GA:/ ){ ## Paired-end
(undef,$number_OB) = split /\t/;
print "Number OB PE >> $number_OB <<\n" if ($verbose);
}
elsif($_ =~ /^CT\/GA:/ ){ ## Single-end
(undef,$number_OB) = split /\t/;
print "Number OB SE >> $number_OB <<\n" if ($verbose);
}
}
if (defined $unique and defined $no_aln and defined $multiple and defined $no_genomic and defined $total_seqs){
warn "Got all necessary information, editing HTML report\n" if ($verbose);
### General Alignment Stats
$doc =~ s/\{\{unique_seqs\}\}/$unique/g;
$doc =~ s/\{\{unique_seqs_text\}\}/$unique_text/g;
$doc =~ s/\{\{no_alignments\}\}/$no_aln/g;
$doc =~ s/\{\{no_alignments_text\}\}/$no_aln_text/g;
$doc =~ s/\{\{multiple_alignments\}\}/$multiple/g;
$doc =~ s/\{\{multiple_alignments_text\}\}/$multiple_text/g;
$doc =~ s/\{\{no_genomic\}\}/$no_genomic/g;
$doc =~ s/\{\{total_sequences_alignments\}\}/$total_seqs/g;
$doc =~ s/\{\{sequences_analysed_in_total\}\}/$total_seq_text/g;
$doc =~ s/\{\{filename\}\}/$input_filename/g;
$doc =~ s/\{\{bismark_version\}\}/$bismark_version/g;
### Alignment stats Plot
$doc =~ s/\{\{alignment_stats_plotly\}\}/$unique,$no_aln,$multiple,$no_genomic/g;
### Strand Origin
$doc =~ s/\{\{number_OT\}\}/$number_OT/g;
$doc =~ s/\{\{number_CTOT\}\}/$number_CTOT/g;
$doc =~ s/\{\{number_CTOB\}\}/$number_CTOB/g;
$doc =~ s/\{\{number_OB\}\}/$number_OB/g;
### Context Methylation
$doc =~ s/\{\{total_C_count\}\}/$total_C_count/g;
### Strand Alignment Plot
$doc =~ s/\{\{strand_alignment_plotly\}\}/$number_OT,$number_CTOT,$number_CTOB,$number_OB/g;
unless (defined $perc_CpG){
$perc_CpG = 'N/A';
}
unless (defined $perc_CHG){
$perc_CHG = 'N/A';
}
unless (defined $perc_CHH){
$perc_CHH = 'N/A';
}
unless (defined $perc_unknown){
$perc_unknown = 'N/A';
}
### Unknown sequence context, just for Bowtie 2 alignments
my $meth_unknown_inject;
my $unmeth_unknown_inject;
my $perc_unknown_inject;
if (defined $meth_unknown){ # if one Unknown context file is present, so should the others
$meth_unknown_inject = " <tr>
<th>Methylated C's in Unknown context</th>
<td>$meth_unknown</td>
</tr>";
$unmeth_unknown_inject = " <tr>
<th>Unmethylated C's in Unknown context</th>
<td>$unmeth_unknown</td>
</tr>";
$perc_unknown_inject = " <tr>
<th>Methylated C's in Unknown context</th>
<td>$perc_unknown%</td>
</tr>";
}
else{
$meth_unknown_inject = $unmeth_unknown_inject = $perc_unknown_inject = '';
}
### injecting this into the table
$doc =~ s/\{\{meth_unknown\}\}/$meth_unknown_inject/g;
$doc =~ s/\{\{unmeth_unknown\}\}/$unmeth_unknown_inject/g;
$doc =~ s/\{\{perc_unknown\}\}/$perc_unknown_inject/g;
$doc =~ s/\{\{meth_CpG\}\}/$meth_CpG/g;
$doc =~ s/\{\{meth_CHG\}\}/$meth_CHG/g;
$doc =~ s/\{\{meth_CHH\}\}/$meth_CHH/g;
$doc =~ s/\{\{unmeth_CpG\}\}/$unmeth_CpG/g;
$doc =~ s/\{\{unmeth_CHG\}\}/$unmeth_CHG/g;
$doc =~ s/\{\{unmeth_CHH\}\}/$unmeth_CHH/g;
$doc =~ s/\{\{perc_CpG\}\}/$perc_CpG/g;
$doc =~ s/\{\{perc_CHG\}\}/$perc_CHG/g;
$doc =~ s/\{\{perc_CHH\}\}/$perc_CHH/g;
my ($perc_CpG_graph, $perc_CHG_graph,$perc_CHH_graph,$perc_unknown_graph);
if ($perc_CpG eq 'N/A'){
$perc_CpG_graph = 0; # values of 0 won't show in the graph and won't produce errors
}
else{
$perc_CpG_graph = $perc_CpG;
}
if ($perc_CHG eq 'N/A'){
$perc_CHG_graph = 0; # values of 0 won't show in the graph and won't produce errors
}
else{
$perc_CHG_graph = $perc_CHG;
}
if ($perc_CHH eq 'N/A'){
$perc_CHH_graph = 0; # values of 0 won't show in the graph and won't produce errors
}
else{
$perc_CHH_graph = $perc_CHH;
}
if ($perc_unknown eq 'N/A'){
$perc_unknown_graph = 0; # values of 0 won't show in the graph and won't produce errors
}
else{
$perc_unknown_graph = $perc_unknown;
}
### Context Methylation Plot
$doc =~ s/\{\{cytosine_methylation_plotly\}\}/$perc_CpG_graph,$perc_CHG_graph,$perc_CHH_graph/g;
}
else{
warn "Am I missing something?\n\n";
}
warn "Complete\n\n";
return $doc;
}
sub read_deduplication_report{
my ($dedup_report,$doc) = @_;
warn "Processing deduplication report $dedup_report ...\n";
open (DEDUP,$dedup_report) or die "Couldn't read from file $dedup_report: $!\n\n";
my $total_seqs;
my $dups;
my $diff_pos;
my $leftover;
while (<DEDUP>){
chomp;
if ($_ =~ /^Total number of alignments/){
(undef,$total_seqs) = split /\t/;
warn "Total number of seqs >> $total_seqs <<\n" if ($verbose);
}
elsif($_ =~ /^Total number duplicated/){
(undef,$dups) = split /\t/;
$dups =~ s/\s.*//; # just need the number, not the percentage
warn "Duplicated >> $dups <<\n" if ($verbose);
}
elsif($_ =~ /^Duplicated alignments were found at/){
(undef,$diff_pos) = split /\t/;
$diff_pos =~ s/\s.*//; # just need the number
warn "Different positions >> $diff_pos <<\n" if ($verbose);
}
elsif($_ =~ /^Total count of deduplicated leftover sequences: (\d+)/){
$leftover = $1;
warn "Leftover seqs >> $leftover <<\n" if ($verbose);
}
}
unless (defined $leftover){
if (defined $dups and defined $total_seqs){
$leftover = $total_seqs - $dups;
}
}
# Checking if we got all we need
if (defined $dups and defined $total_seqs and defined $diff_pos and defined $leftover){
# warn "Got all I need!\n\n";
$doc =~ s/\{\{seqs_total_duplicates\}\}/$total_seqs/g;
$doc =~ s/\{\{unique_alignments_duplicates\}\}/$leftover/g;
$doc =~ s/\{\{duplicate_alignments_duplicates\}\}/$dups/g;
$doc =~ s/\{\{different_positions_duplicates\}\}/$diff_pos/g;
### Duplication Plot Plot.ly
$doc =~ s/\{\{duplication_stats_plotly\}\}/$leftover,$dups/g;
}
else{
warn "Something went wrong... Use --verbose to get a clue...\n";
# skipping this plot entirely if values could not be extracted
return $doc;
}
warn "Complete\n\n";
return $doc;
}
sub read_nucleotide_coverage_report{
my ($nuc_report,$doc) = @_;
warn "Processing nucleotide coverage report '$nuc_report' ...\n";
open (NUC,$nuc_report) or die "Couldn't read from file $nuc_report: $!\n\n";
my %nucs; # storing nucleotides and frequencies
my $linecount = 0;
while (<NUC>){
chomp;
$_ =~ s/\r//; # removing carriage returns
# warn "$_\n"; sleep(1);
my ($element,$count_obs,$observed,$count_exp,$expected,$coverage) = (split /\t/);
# warn "$element , $count_obs , $observed , $count_exp , $expected, $coverage\n"; sleep(1);
if ($linecount == 0){ # verifying that the data appears to be a Bismark nucleotide coverage report
if ($observed eq 'percent sample'){
# warn "Fine, found '$observed'\n";
}
else{
die "Expected to find 'percent sample' as entry in line 1, column 3 but found '$observed'. This doesn't look like a Bismark nucleotide coverage report. Please respecify!\n";
}
if ($expected eq 'percent genomic'){
# warn "Fine, found '$expected'\n";
}
else{
die "Expected to find 'percent genomic' as entry in line 1, column 5 but found '$expected'. This doesn't look like a Bismark nucleotide coverage report. Please respecify!\n";
}
}
else{
$nucs{$element}->{obs}->{percent} = $observed;
$nucs{$element}->{exp}->{percent} = $expected;
$nucs{$element}->{obs}->{counts} = $count_obs;
$nucs{$element}->{exp}->{counts} = $count_exp;
$nucs{$element}->{obs}->{coverage} = $coverage; # coverage of that nucleotide in the sample
warn "Element '$element' observed: $observed\n" if $verbose;
warn "Element '$element' expected: $expected\n" if $verbose;
}
++$linecount;
}
# Checking if we got all we need
my $looksOK = 1;
foreach my $key (keys %nucs){
unless ( (defined $nucs{$key}->{obs}) and (defined $nucs{$key}->{exp})){
$looksOK = 0;
}
}
if ($looksOK){
warn "Got all necessary information, editing HTML report ...\n" if $verbose;
my $minmax = 0;
my @y_array;
my @x_genomic;
my @x_sample;
# foreach my $key (sort {$a cmp $b} keys %nucs){
foreach my $key ('A','T','C','G','AC','CA','TC','CT','CC','CG','GC','GG','AG','GA','TG','GT','TT','TA','AT','AA'){
my $nuc_obs = $nucs{$key}->{obs}->{percent};
my $nuc_exp = $nucs{$key}->{exp}->{percent};
my $counts_obs = $nucs{$key}->{obs}->{counts};
my $counts_exp = $nucs{$key}->{exp}->{counts};
my $cov = $nucs{$key}->{obs}->{coverage};
# calculating log2 observed/expected
my $ratio = $nuc_obs/$nuc_exp;
# my $logratio = sprintf ("%.2f",log($ratio)/log(2));
# if (abs($logratio) > $minmax){
# $minmax = abs($logratio);
# }
warn "$key\tnuc_${key}_obs\t$nuc_obs\tnuc_${key}_exp\t$nuc_exp\tratio: $ratio\n" if $verbose;
$doc =~ s/\{\{nuc_${key}_p_obs\}\}/$nuc_obs/g;
$doc =~ s/\{\{nuc_${key}_p_exp\}\}/$nuc_exp/g;
$doc =~ s/\{\{nuc_${key}_counts_obs\}\}/$counts_obs/g;
$doc =~ s/\{\{nuc_${key}_counts_exp\}\}/$counts_exp/g;
$doc =~ s/\{\{nuc_${key}_coverage\}\}/$cov/g;
# for the plot.ly bargraph
push @y_array, $key;
push @x_genomic, $nuc_exp;
push @x_sample, $nuc_obs;
}
my $y_array = join ("','",@y_array);
my $x_sample = join (" , ",@x_sample);
my $x_genomic = join (" , ",@x_genomic);
$y_array = "'".$y_array."'";
if ($verbose){
print "Y-array: $y_array\n";
print "X genomic array: $x_genomic\n";
print "X sample array: $x_sample\n";
}
$doc =~ s/\{\{nucleo_sample_x\}\}/$x_sample/g;
$doc =~ s/\{\{nucleo_genomic_x\}\}/$x_genomic/g;
$doc =~ s/\{\{nucleo_sample_y\}\}/$y_array/g;
$doc =~ s/\{\{nucleo_genomic_y\}\}/$y_array/g;
# warn "Minimum/maxium ratio was: $minmax\n" if $verbose;
# $doc =~ s/\{\{nuc_minmax\}\}/$minmax/g;
}
else{
warn "Something went wrong, skipping this plot entirely... Use --verbose to get a clue...\n";
# skipping this plot entirely if values could not be extracted
return $doc;
}
warn "Complete\n\n";
return $doc;
}
sub read_splitting_report{
my ($splitting_report,$doc) = @_;
warn "Processing splitting report $splitting_report ...\n";
open (SPLIT,$splitting_report) or die "Couldn't read from file $splitting_report: $!\n\n";
my $total_seqs;
my $total_C_count;
my ($meth_CpG,$meth_CHG,$meth_CHH,$meth_unknown);
my ($unmeth_CpG,$unmeth_CHG,$unmeth_CHH,$unmeth_unknown);
my ($perc_CpG,$perc_CHG,$perc_CHH,$perc_unknown);
while (<SPLIT>){
chomp;
### Context Methylation
if($_ =~ /^Total number of C/ ){
(undef,$total_C_count) = split /\t/;
print "total calls >> $total_C_count <<\n" if ($verbose);
}
elsif($_ =~ /^Total methylated C\'s in CpG context:/ ){
(undef,$meth_CpG) = split /\t/;
print "meth CpG >> $meth_CpG <<\n" if ($verbose);
}
elsif($_ =~ /^Total methylated C\'s in CHG context:/ ){
(undef,$meth_CHG) = split /\t/;
print "meth CHG>> $meth_CHG <<\n" if ($verbose);
}
elsif($_ =~ /^Total methylated C\'s in CHH context:/ ){
(undef,$meth_CHH) = split /\t/;
print "meth CHH >> $meth_CHH <<\n" if ($verbose);
}
elsif($_ =~ /^Total methylated C\'s in Unknown context:/ ){
(undef,$meth_unknown) = split /\t/;
print "meth Unknown >> $meth_unknown <<\n" if ($verbose);
}
elsif($_ =~ /^Total C to T conversions in CpG context:/ ){
(undef,$unmeth_CpG) = split /\t/;
print "unmeth CpG >> $unmeth_CpG <<\n" if ($verbose);
}
elsif($_ =~ /^Total C to T conversions in CHG context:/ ){
(undef,$unmeth_CHG) = split /\t/;
print "unmeth CHG >> $unmeth_CHG <<\n" if ($verbose);
}
elsif($_ =~ /^Total C to T conversions in CHH context:/ ){
(undef,$unmeth_CHH) = split /\t/;
print "unmeth CHH >> $unmeth_CHH <<\n" if ($verbose);
}
elsif($_ =~ /^Total C to T conversions in Unknown context:/ ){
(undef,$unmeth_unknown) = split /\t/;
print "unmeth Unknown >> $unmeth_unknown <<\n" if ($verbose);
}
elsif($_ =~ /^C methylated in CpG context:/ ){
(undef,$perc_CpG) = split /\t/;
$perc_CpG =~ s/%//;
print "percentage CpG >> $perc_CpG <<\n" if ($verbose);
}
elsif($_ =~ /^C methylated in CHG context:/ ){
(undef,$perc_CHG) = split /\t/;
$perc_CHG =~ s/%//;
print "percentage CHG >> $perc_CHG <<\n" if ($verbose);
}
elsif($_ =~ /^C methylated in CHH context:/ ){
(undef,$perc_CHH) = split /\t/;
$perc_CHH =~ s/%//;
print "percentage CHH >> $perc_CHH <<\n" if ($verbose);
}
elsif($_ =~ /^C methylated in Unknown context:/ ){
(undef,$perc_unknown) = split /\t/;
$perc_unknown =~ s/%//;
print "percentage unknown >> $perc_unknown <<\n" if ($verbose);
}
}
if (defined $meth_CpG and defined $meth_CHG and defined $meth_CHH and defined $unmeth_CpG and defined $unmeth_CHG and defined $unmeth_CHH){
warn "Got all necessary information, editing HTML report ...\n" if ($verbose);
### Context Methylation
$doc =~ s/\{\{total_C_count_splitting\}\}/$total_C_count/g;
$doc =~ s/\{\{meth_CpG_splitting\}\}/$meth_CpG/g;
$doc =~ s/\{\{meth_CHG_splitting\}\}/$meth_CHG/g;
$doc =~ s/\{\{meth_CHH_splitting\}\}/$meth_CHH/g;
$doc =~ s/\{\{unmeth_CpG_splitting\}\}/$unmeth_CpG/g;
$doc =~ s/\{\{unmeth_CHG_splitting\}\}/$unmeth_CHG/g;
$doc =~ s/\{\{unmeth_CHH_splitting\}\}/$unmeth_CHH/g;
unless (defined $perc_CpG){
$perc_CpG = 'N/A';
}
unless (defined $perc_CHG){
$perc_CHG = 'N/A';
}
unless (defined $perc_CHH){
$perc_CHH = 'N/A';
}
unless (defined $perc_unknown){
$perc_unknown = 'N/A';
}
### Unknown sequence context, just for Bowtie 2 alignments
my $meth_unknown_inject;
my $unmeth_unknown_inject;
my $perc_unknown_inject;
if (defined $meth_unknown){ # if one Unknown context file is present, so should the others
$meth_unknown_inject = " <tr>
<th>Methylated C's in Unknown context</th>
<td>$meth_unknown</td>
</tr>";
$unmeth_unknown_inject = " <tr>
<th>Unmethylated C's in Unknown context</th>
<td>$unmeth_unknown</td>
</tr>";
$perc_unknown_inject = " <tr>
<th>Methylated C's in Unknown context</th>
<td>$perc_unknown%</td>
</tr>";
}
else{
$meth_unknown_inject = $unmeth_unknown_inject = $perc_unknown_inject = '';
}
### injecting this into the table
$doc =~ s/\{\{meth_unknown_splitting\}\}/$meth_unknown_inject/g;
$doc =~ s/\{\{unmeth_unknown_splitting\}\}/$unmeth_unknown_inject/g;
$doc =~ s/\{\{perc_unknown_splitting\}\}/$perc_unknown_inject/g;
# for the graph we need to take care that there are no N/A values in the percentage fields
my ($perc_CpG_graph, $perc_CHG_graph,$perc_CHH_graph,$perc_unknown_graph);
if ($perc_CpG eq 'N/A'){
$perc_CpG_graph = 0; # values of 0 won't show in the graph and won't produce errors
}
else{
$perc_CpG_graph = $perc_CpG;
}
if ($perc_CHG eq 'N/A'){
$perc_CHG_graph = 0; # values of 0 won't show in the graph and won't produce errors
}
else{
$perc_CHG_graph = $perc_CHG;
}
if ($perc_CHH eq 'N/A'){
$perc_CHH_graph = 0; # values of 0 won't show in the graph and won't produce errors
}
else{
$perc_CHH_graph = $perc_CHH;
}
if ($perc_unknown eq 'N/A'){
$perc_unknown_graph = 0; # values of 0 won't show in the graph and won't produce errors
}
else{
$perc_unknown_graph = $perc_unknown;
}
# Context Methylation post Duplication Graph
$doc =~ s/\{\{cytosine_methylation_post_duplication_plotly\}\}/$perc_CpG_graph,$perc_CHG_graph,$perc_CHH_graph/g;
# Table
$doc =~ s/\{\{perc_CpG_splitting\}\}/$perc_CpG/g;
$doc =~ s/\{\{perc_CHG_splitting\}\}/$perc_CHG/g;
$doc =~ s/\{\{perc_CHH_splitting\}\}/$perc_CHH/g;
}
else{
warn "Am I missing something? Try using --verbose to get a clue...\n\n";
}
warn "Complete\n\n";
return $doc;
}
sub read_mbias_report{
my ($mbias_report,$doc) = @_;
warn "Processing M-bias report $mbias_report ...\n";
open (MBIAS,$mbias_report) or die "Couldn't read from file $mbias_report: $!\n\n";
my %mbias_1;
my %mbias_2;
my $context;
my $read_identity;
my $state = 'single'; # setting this to 'single' if there is no read 2
while (<MBIAS>){
chomp;
if ($_ =~ /^(C.{2}) context/){
$context = $1;
if ($_ =~ /R2/){
$read_identity = 2;
$state = 'paired';
}
else{
$read_identity = 1;
}
# warn "new context is: $context\n";
# warn "Read identity is: Read $read_identity\n";
}
if ($_ =~ /^\d/){
my ($pos,$meth,$unmeth,$perc,$coverage) = (split /\t/);
if ($read_identity == 1){
push @{$mbias_1{$context}->{coverage_x}}, $pos;
push @{$mbias_1{$context}->{coverage_y}}, $coverage;
push @{$mbias_1{$context}->{perc_x}}, $pos;
push @{$mbias_1{$context}->{perc_y}}, $perc;
}
elsif ($read_identity == 2){
push @{$mbias_2{$context}->{coverage_x}}, $pos;
push @{$mbias_2{$context}->{coverage_y}}, $coverage;
push @{$mbias_2{$context}->{perc_x}}, $pos;
push @{$mbias_2{$context}->{perc_y}}, $perc;
}
else{
warn "read identity was unknown : '$read_identity'\n\n";
}
# print join (" ",$pos,$meth,$unmeth,$perc,$coverage)."\n";
}
}
# Read 1 M-bias
my $r1_CpG_coverage_x = join (',',@{$mbias_1{'CpG'}->{coverage_x}});
my $r1_CpG_coverage_y = join (',',@{$mbias_1{'CpG'}->{coverage_y}});
my $r1_CpG_perc_x = join (',',@{$mbias_1{'CpG'}->{perc_x}});
my $r1_CpG_perc_y = join (',',@{$mbias_1{'CpG'}->{perc_y}});
my $r1_CHG_coverage_x = join (',',@{$mbias_1{'CHG'}->{coverage_x}});
my $r1_CHG_coverage_y = join (',',@{$mbias_1{'CHG'}->{coverage_y}});
my $r1_CHG_perc_x = join (',',@{$mbias_1{'CHG'}->{perc_x}});
my $r1_CHG_perc_y = join (',',@{$mbias_1{'CHG'}->{perc_y}});
my $r1_CHH_coverage_x = join (',',@{$mbias_1{'CHH'}->{coverage_x}});
my $r1_CHH_coverage_y = join (',',@{$mbias_1{'CHH'}->{coverage_y}});
my $r1_CHH_perc_x = join (',',@{$mbias_1{'CHH'}->{perc_x}});
my $r1_CHH_perc_y = join (',',@{$mbias_1{'CHH'}->{perc_y}});
# warn "R1 CpG coverage:\n$r1_CpG_coverage_x\n$r1_CpG_coverage_y\nR1 CpG methylation:\n$r1_CpG_perc_x\n$r1_CpG_perc_y\n\n";
# warn "R1 CHG coverage:\n$r1_CHG_coverage_x\n$r1_CHG_coverage_y\nR1 CHG methylation:\n$r1_CHG_perc_x\n$r1_CHG_perc_y\n\n";
# warn "R1 CHH coverage:\n$r1_CHH_coverage_x\n$r1_CHH_coverage_y\nR1 CHH methylation:\n$r1_CHH_perc_x\n$r1_CHH_perc_y\n\n";
# CpG
$doc =~ s/\{\{mbias1_CpG_meth_x\}\}/$r1_CpG_perc_x/g;
$doc =~ s/\{\{mbias1_CpG_meth_y\}\}/$r1_CpG_perc_y/g;
$doc =~ s/\{\{mbias1_CpG_coverage_x\}\}/$r1_CpG_coverage_x/g;
$doc =~ s/\{\{mbias1_CpG_coverage_y\}\}/$r1_CpG_coverage_y/g;
# CHG
$doc =~ s/\{\{mbias1_CHG_meth_x\}\}/$r1_CHG_perc_x/g;
$doc =~ s/\{\{mbias1_CHG_meth_y\}\}/$r1_CHG_perc_y/g;
$doc =~ s/\{\{mbias1_CHG_coverage_x\}\}/$r1_CHG_coverage_x/g;
$doc =~ s/\{\{mbias1_CHG_coverage_y\}\}/$r1_CHG_coverage_y/g;
# CHH
$doc =~ s/\{\{mbias1_CHH_meth_x\}\}/$r1_CHH_perc_x/g;
$doc =~ s/\{\{mbias1_CHH_meth_y\}\}/$r1_CHH_perc_y/g;
$doc =~ s/\{\{mbias1_CHH_coverage_x\}\}/$r1_CHH_coverage_x/g;
$doc =~ s/\{\{mbias1_CHH_coverage_y\}\}/$r1_CHH_coverage_y/g;
# Read 2 M-bias
if (%mbias_2){
my $r2_CpG_coverage_x = join (',',@{$mbias_2{'CpG'}->{coverage_x}});
my $r2_CpG_coverage_y = join (',',@{$mbias_2{'CpG'}->{coverage_y}});
my $r2_CpG_perc_x = join (',',@{$mbias_2{'CpG'}->{perc_x}});
my $r2_CpG_perc_y = join (',',@{$mbias_2{'CpG'}->{perc_y}});
my $r2_CHG_coverage_x = join (',',@{$mbias_2{'CHG'}->{coverage_x}});
my $r2_CHG_coverage_y = join (',',@{$mbias_2{'CHG'}->{coverage_y}});
my $r2_CHG_perc_x = join (',',@{$mbias_2{'CHG'}->{perc_x}});
my $r2_CHG_perc_y = join (',',@{$mbias_2{'CHG'}->{perc_y}});
my $r2_CHH_coverage_x = join (',',@{$mbias_2{'CHH'}->{coverage_x}});
my $r2_CHH_coverage_y = join (',',@{$mbias_2{'CHH'}->{coverage_y}});
my $r2_CHH_perc_x = join (',',@{$mbias_2{'CHH'}->{perc_x}});
my $r2_CHH_perc_y = join (',',@{$mbias_2{'CHH'}->{perc_y}});
# warn "r2 CpG coverage:\n$r2_CpG_coverage_x\n$r2_CpG_coverage_y\nr2 CpG methylation:\n$r2_CpG_perc_x\n$r2_CpG_perc_y\n\n";
# warn "r2 CHG coverage:\n$r2_CHG_coverage_x\n$r2_CHG_coverage_y\nr2 CHG methylation:\n$r2_CHG_perc_x\n$r2_CHG_perc_y\n\n";
# warn "r2 CHH coverage:\n$r2_CHH_coverage_x\n$r2_CHH_coverage_y\nr2 CHH methylation:\n$r2_CHH_perc_x\n$r2_CHH_perc_y\n\n";
# CpG
$doc =~ s/\{\{mbias2_CpG_meth_x\}\}/$r2_CpG_perc_x/g;
$doc =~ s/\{\{mbias2_CpG_meth_y\}\}/$r2_CpG_perc_y/g;
$doc =~ s/\{\{mbias2_CpG_coverage_x\}\}/$r2_CpG_coverage_x/g;
$doc =~ s/\{\{mbias2_CpG_coverage_y\}\}/$r2_CpG_coverage_y/g;
# CHG
$doc =~ s/\{\{mbias2_CHG_meth_x\}\}/$r2_CHG_perc_x/g;
$doc =~ s/\{\{mbias2_CHG_meth_y\}\}/$r2_CHG_perc_y/g;
$doc =~ s/\{\{mbias2_CHG_coverage_x\}\}/$r2_CHG_coverage_x/g;
$doc =~ s/\{\{mbias2_CHG_coverage_y\}\}/$r2_CHG_coverage_y/g;
# CHH
$doc =~ s/\{\{mbias2_CHH_meth_x\}\}/$r2_CHH_perc_x/g;
$doc =~ s/\{\{mbias2_CHH_meth_y\}\}/$r2_CHH_perc_y/g;
$doc =~ s/\{\{mbias2_CHH_coverage_x\}\}/$r2_CHH_coverage_x/g;
$doc =~ s/\{\{mbias2_CHH_coverage_y\}\}/$r2_CHH_coverage_y/g;
}
else {
$doc =~ s/\{\{bm_mbias_2\}\}/false/g;