-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreStrainingReport
executable file
·687 lines (546 loc) · 27.1 KB
/
reStrainingReport
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
#!/usr/bin/env perl
use warnings;
use strict;
use Getopt::Long;
use FindBin qw($RealBin);
use lib "$RealBin/../lib";
## This program is Copyright (C) 2018-23, Felix Krueger ([email protected])
## 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 $report_version = 'v0.4.0';
my (@reStrainingOrder_summary_reports);
my ($output_dir,$verbose,$manual_output_file,$candidates) = 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";
my %strain_scores;
my %hybrid_scores;
my %allele_ratios;
while (@reStrainingOrder_summary_reports){
# clearing
%strain_scores = ();
%hybrid_scores = ();
%allele_ratios = ();
my $summary_report = shift @reStrainingOrder_summary_reports;
### HTML OUTPUT FILE
my $report_output = $summary_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 reStrainingOrder HTML report to >> $report_output <<\n\n";
# Get the report template into a string
my $doc = read_report_template('plotly_template.tpl');
$doc =~ s/\{\{report_version\}\}/$report_version/g;
my $logo = read_report_template('reStrainingOrder.logo');
if ($doc =~ s/\{\{logo_goes_here\}\}.*\{\{logo_goes_here\}\}/$logo/s){ # treating the string as a single line
warn "reStrainingOrder.logo injection successful!\n" if $verbose;
}
my $summary_report_bam_file = $summary_report;
$summary_report_bam_file =~ s/reStrainingOrder.summary_stats.txt/bam/;
$doc =~ s/\{\{filename\}\}/$summary_report_bam_file/g;
# 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 $reStrainingOrder_logo = read_report_template('reStrainingOrder.logo');
#if ($doc =~ s/\{\{rSO_logo_goes_here\}\}.*\{\{rSO_logo_goes_here\}\}/$reStrainingOrder_logo/s){ # treating the string as a single line
# warn "reStrainingOrder.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);
# PROCESSING reStrainingOrder SUMMARY REPORTS
warn "="x110,"\n";
warn "Using the following reStrainingOrder summary report:\t\t> $summary_report <\n";
$doc = read_rSO_summary_report($summary_report,$doc);
# It is important to have the same order for the Hybrid Scores and allele ratios, which will be handled in this subroutine
adjust_allele_ratio_order();
################################################
### PROCESSING GENERAL STATISTICS
my $number_of_positions_covered;
# $number_of_positions_covered .= "<tr>\n\t<td>Input filename:</td>\n\t<td>$summary_report_bam_file</td>\n</tr>\n";
$number_of_positions_covered .= "<tr>\n<td>Different positions on chr1 covered:</td>\n";
foreach my $number(sort {$a<=>$b} keys %strain_scores){
$number_of_positions_covered .= "\t<td>$strain_scores{$number}->{covered}</td>\n";
$number_of_positions_covered .= "\t</tr>\n";
last; # only needed once
}
$doc =~ s/\{\{number_of_positions_covered\}\}/$number_of_positions_covered/g;
################################################
### PROCESSING SINGLE STRAIN COMPTIBILITY SCORES
my $count = 0;
my $strain_compatibility_content;
my $strain_scores_strains_plotly;
my $strain_scores_percentages_plotly;
$strain_compatibility_content .= "<tbody>\n";
$strain_compatibility_content .= "\t<tr>\n";
$strain_compatibility_content .= "\t\t<th>Strain</th>\n";
$strain_compatibility_content .= "\t\t<th>Agreeing Calls</th>\n";
$strain_compatibility_content .= "\t\t<th>Disagreeing Calls</th>\n";
$strain_compatibility_content .= "\t\t<th>Compatibility Percentage</th>\n";
$strain_compatibility_content .= "\t</tr>\n";
$strain_compatibility_content .= "</tbody>\n";
$strain_compatibility_content .= "<tbody>\n";
# warn "\n\nContents of \%strain_scores:\n";
foreach my $number(sort {$a<=>$b} keys %strain_scores){
$strain_compatibility_content .= "\t<tr>\n";
$strain_compatibility_content .= "\t\t<td>$strain_scores{$number}->{strain}</td>\n";
$strain_compatibility_content .= "\t\t<td>$strain_scores{$number}->{agree}</td>\n";
$strain_compatibility_content .= "\t\t<td>$strain_scores{$number}->{disagree}</td>\n";
$strain_compatibility_content .= "\t\t<td>$strain_scores{$number}->{percent}</td>\n";
$strain_compatibility_content .= "\t</tr>\n";
$strain_scores_strains_plotly .= " '$strain_scores{$number}->{strain}' ,";
$strain_scores_percentages_plotly .= " $strain_scores{$number}->{percent} ,";
# warn "$number\t$strain_scores{$number}->{strain}\t$strain_scores{$number}->{covered}\t$strain_scores{$number}->{agree}\t$strain_scores{$number}->{disagree}\t$strain_scores{$number}->{percent}\n";
++$count;
}
# remove trailing comma
$strain_scores_strains_plotly =~ s/,$//;
$strain_scores_percentages_plotly =~ s/,$//;
# warn "$strain_scores_strains_plotly\n$strain_scores_percentages_plotly\n"; sleep(5);
$strain_compatibility_content .= "</tbody>\n";
# warn "$strain_compatibility_content\n";
# injecting this into the HTML template
$doc =~ s/\{\{strain_compatibility_content\}\}/$strain_compatibility_content/g;
$doc =~ s/\{\{strain_scores_strains_plotly\}\}/$strain_scores_strains_plotly/g;
$doc =~ s/\{\{strain_scores_percentages_plotly\}\}/$strain_scores_percentages_plotly/g;
################################################
### PROCESSING SINGLE STRAIN COMPATIBILITY CONFIDENCE
$count = 0;
my $strain_compatibility_confidence;
$strain_compatibility_confidence .= "<tbody>\n";
$strain_compatibility_confidence .= "\t<tr>\n";
$strain_compatibility_confidence .= "\t\t<th>Strain</th>\n";
$strain_compatibility_confidence .= "\t\t<th>Covered Positions</th>\n";
$strain_compatibility_confidence .= "\t\t<th>Compatibility Percentage</th>\n";
$strain_compatibility_confidence .= "\t</tr>\n";
$strain_compatibility_confidence .= "</tbody>\n";
$strain_compatibility_confidence .= "<tbody>\n";
# warn "\n\nContents of \%strain_scores:\n";
foreach my $number(sort {$a<=>$b} keys %strain_scores){
last if ($count == $candidates);
$strain_compatibility_confidence .= "\t<tr>\n";
$strain_compatibility_confidence .= "\t\t<td>$strain_scores{$number}->{strain}</td>\n";
$strain_compatibility_confidence .= "\t\t<td>$strain_scores{$number}->{covered}</td>\n";
$strain_compatibility_confidence .= "\t\t<td>$strain_scores{$number}->{percent}</td>\n";
$strain_compatibility_confidence .= "\t</tr>\n";
# warn "$number\t$strain_scores{$number}->{strain}\t$strain_scores{$number}->{covered}\t$strain_scores{$number}->{agree}\t$strain_scores{$number}->{disagree}\t$strain_scores{$number}->{percent}\n";
++$count;
}
$strain_compatibility_confidence .= "</tbody>\n";
# warn "$strain_compatibility_confidence\n";
# injecting this into the HTML template
$doc =~ s/\{\{strain_compatibility_confidence\}\}/$strain_compatibility_confidence/g;
##########################################
### PROCESSING HYBRID COMPATIBILITY SCORES
my $hybrid_compatibility_content;
my $hybrid_compatibility_strains_plotly;
my $hybrid_percentages_strain1_plotly;
my $hybrid_percentages_strain2_plotly;
$hybrid_compatibility_content .= "<tbody>\n";
$hybrid_compatibility_content .= "\t<tr>\n";
$hybrid_compatibility_content .= "\t\t<th>Potential Hybrid</th>\n";
$hybrid_compatibility_content .= "\t\t<th>Agreeing Calls</th>\n";
$hybrid_compatibility_content .= "\t\t<th>Disagreeing Calls</th>\n";
$hybrid_compatibility_content .= "\t\t<th>Compatibility %</th>\n";
$hybrid_compatibility_content .= "\t\t<th>Strain1 %</th>\n";
$hybrid_compatibility_content .= "\t\t<th>Strain2 %</th>\n";
$hybrid_compatibility_content .= "\t</tr>\n";
$hybrid_compatibility_content .= "</tbody>\n";
$hybrid_compatibility_content .= "<tbody>\n";
$count = 0;
# warn "\n\nContents of \%hybrid_scores:\n";
foreach my $number(sort {$a<=>$b} keys %hybrid_scores){
last if ($count == $candidates);
$hybrid_compatibility_content .= "\t<tr>\n";
$hybrid_compatibility_content .= "\t\t<td>$hybrid_scores{$number}->{hybrid}</td>\n";
$hybrid_compatibility_content .= "\t\t<td>$hybrid_scores{$number}->{agree}</td>\n";
$hybrid_compatibility_content .= "\t\t<td>$hybrid_scores{$number}->{disagree}</td>\n";
$hybrid_compatibility_content .= "\t\t<td>$hybrid_scores{$number}->{percent}</td>\n";
$hybrid_compatibility_content .= "\t\t<td>$allele_ratios{$number}->{strain1_percent}</td>\n";
$hybrid_compatibility_content .= "\t\t<td>$allele_ratios{$number}->{strain2_percent}</td>\n";
$hybrid_compatibility_content .= "\t</tr>\n";
#warn "$number\t$hybrid_scores{$number}->{hybrid}\t$hybrid_scores{$number}->{agree}\t$hybrid_scores{$number}->{disagree}\t$hybrid_scores{$number}->{percent}\n";
++$count;
# warn "$hybrid_scores{$number}->{hybrid}\t$hybrid_scores{$number}->{percent}\t$allele_ratios{$number}->{strain1_percent}\t$allele_ratios{$number}->{strain2_percent}\n";
my $adjusted_s1 = sprintf("%.2f",$hybrid_scores{$number}->{percent} * $allele_ratios{$number}->{strain1_percent} / 100);
my $adjusted_s2 = sprintf("%.2f",$hybrid_scores{$number}->{percent} * $allele_ratios{$number}->{strain2_percent} / 100);
#warn "// $hybrid_scores{$number}->{hybrid}\t$allele_ratios{$number}->{strain1_percent}\t$allele_ratios{$number}->{strain2_percent}\t$adjusted_s1\t$adjusted_s2\n";
#sleep(1);
$hybrid_compatibility_strains_plotly .= " '$hybrid_scores{$number}->{hybrid}' ,";
$hybrid_percentages_strain1_plotly .= " $adjusted_s1 ,";
$hybrid_percentages_strain2_plotly .= " $adjusted_s2 ,";
}
$hybrid_compatibility_content .= "</tbody>\n";
# warn "$hybrid_compatibility_content\n";
# remove trailing comma
$hybrid_compatibility_strains_plotly =~ s/,$//;
$hybrid_percentages_strain1_plotly =~ s/,$//;
$hybrid_percentages_strain2_plotly =~ s/,$//;
# injecting this into the HTML template
# For the table
$doc =~ s/\{\{hybrid_compatibility_content\}\}/$hybrid_compatibility_content/g;
# For the stacked bar graph
$doc =~ s/\{\{hybrid_compatibility_strains_plotly\}\}/$hybrid_compatibility_strains_plotly/g;
$doc =~ s/\{\{hybrid_percentages_strain1_plotly\}\}/$hybrid_percentages_strain1_plotly/g;
$doc =~ s/\{\{hybrid_percentages_strain2_plotly\}\}/$hybrid_percentages_strain2_plotly/g;
# warn "$hybrid_compatibility_strains_plotly\n$hybrid_percentages_strain1_plotly\n$hybrid_percentages_strain2_plotly\n";
####################################
### PROCESSING ALLELE RATIOS SECTION
my $allelic_ratio_content;
my $strain_names_ratio_plotly;
my $strain1_ratio_plotly;
my $strain2_ratio_plotly;
$allelic_ratio_content .= "<tbody>\n";
$allelic_ratio_content .= "\t<tr>\n";
$allelic_ratio_content .= "\t\t<th>Strain1</th>\n";
$allelic_ratio_content .= "\t\t<th>Strain1 Calls</th>\n";
$allelic_ratio_content .= "\t\t<th>Strain1 %</th>\n";
$allelic_ratio_content .= "\t\t<th>Strain2</th>\n";
$allelic_ratio_content .= "\t\t<th>Strain2 Calls</th>\n";
$allelic_ratio_content .= "\t\t<th>Strain2 %</th>\n";
$allelic_ratio_content .= "\t</tr>\n";
$allelic_ratio_content .= "</tbody>\n";
$allelic_ratio_content .= "<tbody>\n";
$count = 0;
# warn "\n\nContents of \%allele_ratios:\n";
foreach my $number(sort {$a<=>$b} keys %allele_ratios){
last if ($count == $candidates);
# warn "$number\t$allele_ratios{$number}->{strain1}\t$allele_ratios{$number}->{strain2}\t$allele_ratios{$number}->{strain1_count}\t$allele_ratios{$number}->{strain2_count}\t$allele_ratios{$number}->{strain1_percent}\t$allele_ratios{$number}->{strain2_percent}\n";
# sleep(1);
++$count;
$allelic_ratio_content .= "\t<tr>\n";
$allelic_ratio_content .= "\t\t<td>$allele_ratios{$number}->{strain1}</td>\n";
$allelic_ratio_content .= "\t\t<td>$allele_ratios{$number}->{strain1_count}</td>\n";
$allelic_ratio_content .= "\t\t<td>$allele_ratios{$number}->{strain1_percent}</td>\n";
$allelic_ratio_content .= "\t\t<td>$allele_ratios{$number}->{strain2}</td>\n";
$allelic_ratio_content .= "\t\t<td>$allele_ratios{$number}->{strain2_count}</td>\n";
$allelic_ratio_content .= "\t\t<td>$allele_ratios{$number}->{strain2_percent}</td>\n";
$allelic_ratio_content .= "\t</tr>\n";
$strain_names_ratio_plotly .= " '$allele_ratios{$number}->{strain1}/$allele_ratios{$number}->{strain2}' ,";
$strain1_ratio_plotly .= " $allele_ratios{$number}->{strain1_count} ,";
$strain2_ratio_plotly .= " $allele_ratios{$number}->{strain2_count} ,";
}
$allelic_ratio_content .= "</tbody>\n";
# warn "$allelic_ratio_content\n";
# remove trailing commas
$strain_names_ratio_plotly =~ s/,$//;
$strain1_ratio_plotly =~ s/,$//;
$strain2_ratio_plotly =~ s/,$//;
# warn "$strain_names_ratio_plotly\n$strain1_ratio_plotly\n$strain2_ratio_plotly\n";
# injecting this into the HTML template
$doc =~ s/\{\{allelic_ratio_content\}\}/$allelic_ratio_content/g;
$doc =~ s/\{\{strain_names_ratio_plotly\}\}/$strain_names_ratio_plotly/g;
$doc =~ s/\{\{strain1_ratio_y_plotly\}\}/$strain1_ratio_plotly/g;
$doc =~ s/\{\{strain2_ratio_y_plotly\}\}/$strain2_ratio_plotly/g;
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 adjust_allele_ratio_order{
# warn "Need to get the allele-ratios into the same order as the hybrid-score table\n";
my %temp_ratios;
foreach my $entry (sort {$a<=>$b} keys %allele_ratios ){
my $newkey = "$allele_ratios{$entry}->{strain1}/$allele_ratios{$entry}->{strain2}";
$temp_ratios{$newkey}->{strain1} = $allele_ratios{$entry}->{strain1};
$temp_ratios{$newkey}->{strain2} = $allele_ratios{$entry}->{strain2};
$temp_ratios{$newkey}->{strain1_count} = $allele_ratios{$entry}->{strain1_count};
$temp_ratios{$newkey}->{strain2_count} = $allele_ratios{$entry}->{strain2_count};
$temp_ratios{$newkey}->{strain1_percent} = $allele_ratios{$entry}->{strain1_percent};
$temp_ratios{$newkey}->{strain2_percent} = $allele_ratios{$entry}->{strain2_percent};
}
# Resetting %allele_ratios to change the order from alphabetically sorted to the same order as the hybrid compatibility
%allele_ratios = ();
my $new_order = 0;
foreach my $entry (sort {$a<=>$b} keys %hybrid_scores){
if (exists $temp_ratios{$hybrid_scores{$entry}->{hybrid}}){
$new_order++;
$allele_ratios{$new_order}->{strain1} = $temp_ratios{$hybrid_scores{$entry}->{hybrid}}->{strain1};
$allele_ratios{$new_order}->{strain2} = $temp_ratios{$hybrid_scores{$entry}->{hybrid}}->{strain2};
$allele_ratios{$new_order}->{strain1_count} = $temp_ratios{$hybrid_scores{$entry}->{hybrid}}->{strain1_count};
$allele_ratios{$new_order}->{strain2_count} = $temp_ratios{$hybrid_scores{$entry}->{hybrid}}->{strain2_count};
$allele_ratios{$new_order}->{strain1_percent} = $temp_ratios{$hybrid_scores{$entry}->{hybrid}}->{strain1_percent};
$allele_ratios{$new_order}->{strain2_percent} = $temp_ratios{$hybrid_scores{$entry}->{hybrid}}->{strain2_percent};
# print "$entry\t$new_order\t$hybrid_scores{$entry}->{hybrid}\t$allele_ratios{$new_order}->{strain1_percent}\t$allele_ratios{$new_order}->{strain2_percent}\n";
}
else{
warn "We've got a problem with the hybrid name!";
}
}
}
sub read_rSO_summary_report{
my ($summary_report,$doc) = @_;
warn "Processing reStrainingOrder summary report $summary_report ...\n";
open (SUMM,$summary_report) or die "Couldn't read from file $summary_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 $processed_strain_scores;
my $processed_hybrid_scores;
my $processed_allele_ratios;
my $strain_order = 0;
my $hybrid_order = 0;
my $allele_ratio_order = 0;
while (<SUMM>){
chomp;
unless (defined $processed_strain_scores){
# this must the first line
if($_ eq 'STRAIN SCORES'){
# warn "Fine, this looks like a proper reStrainingOrder summary report\n";
++$processed_strain_scores;
next;
}
else{
die "This file does not look like a reStrainingOrder summary report. Aborting... (First line was: >$_<\n\n ";
}
}
# warn "$_\n";
if ($_ eq ""){
# warn "Line is empty: skipping\n\n";
next;
}
if (defined $processed_hybrid_scores){
# warn "Now processing the Hybrid Scores section\n";
# this has to be Section 1: Strain Scores
if (defined $processed_allele_ratios){
# this has to be Section 3: Allele Ratios
# warn "ALLELE RATIOS: $_\n"; # sleep(1);
if ($_ =~ /^Strain1/){
# warn "Header, skipping...: $_\n"; sleep(1);
next; # header
}
else{
my ($strain1,$strain1_count,$strain1_percent,$strain2,$strain2_count,$strain2_percent) = (split /\t/);
++$allele_ratio_order;
print join (" // ",$allele_ratio_order,$strain1,$strain2_count,$strain1_percent,$strain2,$strain2_count,$strain2_percent),"\n" if $verbose; # sleep(1);
$allele_ratios{$allele_ratio_order}->{strain1} = $strain1;
$allele_ratios{$allele_ratio_order}->{strain2} = $strain2;
$allele_ratios{$allele_ratio_order}->{strain1_count} = $strain1_count;
$allele_ratios{$allele_ratio_order}->{strain2_count} = $strain2_count;
$allele_ratios{$allele_ratio_order}->{strain1_percent} = $strain1_percent;
$allele_ratios{$allele_ratio_order}->{strain2_percent} = $strain2_percent;
}
}
else{
if ($_ eq 'ALLELE RATIOS'){
# warn "Reached the Allele Ratio section\n";
# sleep(1);
++$processed_allele_ratios;
next;
}
else{
# this has to be Section 2: Hybrid Scores
# warn "HYBRID SCORES: $_\n";
if ($_ =~ /^Potential/){
# warn "Header, skipping...\n";
next; # header
}
else{
my ($hybrid,$agree,$disagree,$percent,$strain1_index,$strain2_index) = (split /\t/);
++$hybrid_order;
print join (" // ",$hybrid_order,$hybrid,$agree,$disagree,$percent,$strain1_index,$strain2_index),"\n" if $verbose; # sleep(1);
$hybrid_scores{$hybrid_order}->{hybrid} = $hybrid;
$hybrid_scores{$hybrid_order}->{strain1_index} = $strain1_index;
$hybrid_scores{$hybrid_order}->{strain2_index} = $strain2_index;
$hybrid_scores{$hybrid_order}->{agree} = $agree;
$hybrid_scores{$hybrid_order}->{disagree} = $disagree;
$hybrid_scores{$hybrid_order}->{percent} = $percent;
}
}
}
}
else{
# this has to be Section 1: Strain Scores
if ($_ eq 'HYBRID SCORES'){
# warn "Reached the Hybrid Score section\n";
++$processed_hybrid_scores;
next;
}
else{
# warn "STRAIN SCORES: $_\n";
if ($_ =~ /^Strain/){
# warn "Header, skipping...\n";
next; # header
}
else{
my ($strain,$covered,$agree,$disagree,$percent) = (split /\t/);
++$strain_order;
print join (" // ",$strain_order,$strain,$covered,$agree,$disagree,$percent),"\n" if $verbose; # sleep(1);
$strain_scores{$strain_order}->{strain} = $strain;
$strain_scores{$strain_order}->{covered} = $covered;
$strain_scores{$strain_order}->{agree} = $agree;
$strain_scores{$strain_order}->{disagree} = $disagree;
$strain_scores{$strain_order}->{percent} = $percent;
}
}
}
}
warn "Complete\n\n";
return $doc;
}
sub read_report_template{
my $template = shift;
my $doc;
warn "Attempting to open file from: $RealBin/plotly/$template\n\n" if ($verbose);
open (DOC,"$RealBin/plotly/$template") or die "Failed to find file $template: $!";
while(<DOC>){
chomp;
$_ =~ s/\r//g;
$doc .= $_."\n";
}
close DOC or warn $!;
return $doc;
}
sub process_commandline{
my $help;
my $output_dir;
my $manual_output_file;
my $summary_report;
my $candidates;
my $verbose;
my $version;
my $command_line = GetOptions ('help|man' => \$help,
'dir=s' => \$output_dir,
'o|output=s' => \$manual_output_file,
'summary_report=s' => \$summary_report,
'version' => \$version,
'candidates=i' => \$candidates,
'verbose' => \$verbose,
);
### EXIT ON ERROR if there were errors with any of the supplied options
unless ($command_line){
die "Please re-specify command line options\n";
}
### HELPFILE
if ($help){
print_helpfile();
exit;
}
if ($version){
print << "VERSION";
reStrainingOrder HTML Report Module
Version: $report_version
Copyright 2018-23, Felix Krueger
https://github.com/FelixKrueger/reStrainingOrder
VERSION
exit;
}
### OUTPUT DIR PATH
if (defined $output_dir){
unless ($output_dir eq ''){ # if the output dir has been passed on by the methylation extractor and is an empty string we don't want to change it
unless ($output_dir =~ /\/$/){
$output_dir =~ s/$/\//;
}
}
}
else{
$output_dir = '';
}
## First we are looking for alignment reports, and then look whether there are any optional plots with the same base name
if ($summary_report){
### we only process the one alignment report (and possibly the other ones as well) that was specified
push @reStrainingOrder_summary_reports, $summary_report;
}
else{ ### no summary report specified, looking in the current directory for file ending in *reStrainingOrder.summary_stats.txt
### looking in the current directory for report files. Less than 1 report file is not allowed
@reStrainingOrder_summary_reports = <*reStrainingOrder.summary_stats.txt>;
if (scalar @reStrainingOrder_summary_reports == 0){
warn "Found no potential reStrainingOrder summary reports in the current directory. Please specify a single reStrainingOrder summary report file using the option '--summary_report FILE'\n\n";
print_helpfile();
exit;
}
else{
# there are reStrainingOrder summary report(s) in the directory
warn "Found ",scalar @reStrainingOrder_summary_reports," summary reports in current directory. Now processing all of them.\n";
}
}
### Ensuring that there isn't more than 1 file in @reStrainingOrder_summary_reports if someone manually specified an output file.
if (scalar @reStrainingOrder_summary_reports > 1){
if (defined $manual_output_file){
die "You cannot run reStrainingReport on more than 1 file while specifying a single output file. Either lose the option -o to derive the output filenames automatically, or specify a single reStrainingOrder summary report file using the option '--summary_report FILE'\n\n";
}
}
if (defined $candidates){
unless ($candidates > 0 and $candidates <= 30){
die "Please select a number of candidates between 1 and 30 (the default is 10) and try again\n";
}
warn "Reporting >>$candidates<< potential hybrid combinations [user defined]\n";
}
else{
# the default number of candidates is 10
$candidates = 30;
warn "Reporting >>$candidates<< potential hybrid combinations [default]\n";
}
return ($output_dir,$verbose,$manual_output_file,$candidates);
}
sub print_helpfile{
print <<EOF
SYNOPSIS:
This script uses one or more reStrainingOrder summary_stats.txt reports to generate a graphical HTML report page. If several
reStrainingOrder reports are found in the same folder, a separate report will be generated for each of these, whereby the output filename
will be derived from the original file.
USAGE: reStrainingReport [options]
-o/--output <filename> Name of the output file (optional). If not specified explicitly, the output filename(s) will be derived
from the reStrainingOrder.summary_stats file(s). Specifying an output filename only works if the HTML
report is to be generated for a single reStrainingOrder report.
--dir Output directory (needs to exist). Output is written to the current directory if not specified explicitly.
--summary_report FILE If not specified explicitly, reStrainingReport attempts to find all reStrainingOrder summary_stats report file(s)
in the current working directory and produces a separate HTML report for each file.
Script last modified: 11 April 2023
EOF
;
exit 1;
}