forked from Molmed/sisyphus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateReport.pl
executable file
·595 lines (495 loc) · 20.8 KB
/
generateReport.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/perl -w
use FindBin; # Find the script location
use lib "$FindBin::Bin/lib";# Add the script libdir to libs
use Molmed::Sisyphus::Libpath;
use strict;
use Getopt::Long;
use Pod::Usage;
use Cwd qw(abs_path cwd);
use File::Basename;
use File::Find;
use File::Copy qw/cp move/;
use XML::Simple;
use XML::LibXSLT;
use XML::LibXML;
use Molmed::Sisyphus::Common qw(mkpath);
use Molmed::Sisyphus::QStat;
use Molmed::Sisyphus::Plot;
=pod
=head1 NAME
generateReport.pl - Create a top level report on the runfolder
=head1 SYNOPSIS
generateReport.pl -help|-man
generateReport.pl -runfolder <runfolder> [-debug]
=head1 OPTIONS
=over 4
=item -h|-help
prints out a brief help text.
=item -m|-man
Opens the manpage.
=item -runfolder
The runfolder to generate report on.
=item -debug
Print debugging information
=back
=head1 DESCRIPTION
Collects statistics and other data into the folder Summary, which
can then be copied to the GA-summaries folder for future reference.
Creates a top-level report with various metrics about the run.
extractProject.pl has to be run first so that the project reports
can be included in the Summary folder and linked from the global report.
=cut
# Parse options
my($help,$man) = (0,0);
my($rfPath) = (undef);
our($debug) = 0;
GetOptions('help|?'=>\$help,
'man'=>\$man,
'runfolder=s' => \$rfPath,
'debug' => \$debug,
) or pod2usage(-verbose => 0);
pod2usage(-verbose => 1) if ($help);
pod2usage(-verbose => 2) if ($man);
unless(defined $rfPath && -e $rfPath){
print STDERR "Runfolder not specified or does not exist\n";
pod2usage(-verbose => 1);
exit;
}
# Create a new sisyphus object for common functions
my $sisyphus = Molmed::Sisyphus::Common->new(PATH=>$rfPath, DEBUG=>$debug);
$rfPath = $sisyphus->PATH;
my $machineType = $sisyphus->machineType();
# Set the output path
my $outDir = "$rfPath/Summary";
# Create the output directory
if(-e "$outDir"){
my $t = time();
move("$outDir", "$outDir.old.$t") or die "Failed to rename old Summary folder: $!\n";
}
mkpath("$outDir", 2770) or die "Failed to create dir '$rfPath/Summary': $!\n";
$outDir = abs_path($outDir);
print STDERR "Summary directory: $outDir\n" if($debug);
my $plotter = Molmed::Sisyphus::Plot->new();
# Read the sample sheet
my $sampleSheet = $sisyphus->readSampleSheet();
my @projects = sort keys %{$sampleSheet};
push @projects, 'Undetermined_indices' if(-e "$rfPath/Statistics/Project_Undetermined_indices");
# Get the statistics generated by RTA/CASAVA
my($RtaLaneStats,$RtaSampleStats) = $sisyphus->resultStats();
# assume the same offset for all files (set in the loop below)
my $offset = 0;
# Read all stats from the project dirs
my %dumps;
foreach my $project (@projects){
my $projDir = "$rfPath/Statistics/Project_$project";
unless(-e "$projDir/extractProject.complete" || -e "$projDir/extractProject.complete.gz"){
unless($project eq 'Undetermined_indices'){
die "$project has not been extracted yet. Aborting\n";
}
}
if(-e $projDir){
opendir(my $pDirFh, $projDir) or die "Failed to open '$projDir': $!\n";
foreach my $sampleDir (grep /^[^\.]/, readdir($pDirFh)){
next unless (-d "$projDir/$sampleDir");
opendir(my $sDirFh, "$projDir/$sampleDir") or die "Failed to open '$projDir/$sampleDir': $!\n";
foreach my $statfile (grep /\.statdump.zip$/, readdir($sDirFh)){
my $stat = Molmed::Sisyphus::QStat->new(DEBUG=>$debug);
$stat->loadData("$projDir/$sampleDir/$statfile");
my $lane = $stat->{LANE};
$offset = $stat->{OFFSET} unless($offset);
push @{$dumps{$project}->{$lane}}, $stat;
}
closedir($sDirFh);
}
closedir($pDirFh);
}
}
open(my $reportFh, '>', "$outDir/summaryReport.xml");
print $reportFh q(<?xml version="1.0"?>), "\n";
print $reportFh q(<?xml-stylesheet type="text/xsl" href="summaryReport.xsl"?>), "\n";
print $reportFh q(<SummaryReport xmlns="illuminareport.xml.molmed">), "\n";
my $xs = XML::Simple->new(RootName=>undef);
if($sisyphus->machineType() eq 'miseq'){
cp("$FindBin::Bin/summaryReportMiSeq.xsl", "$outDir/summaryReport.xsl")
or die "Failed to copy '$FindBin::Bin/summaryReportMiSeq.xsl' to '$outDir/summaryReport.xsl': $!\n";
}else{
cp("$FindBin::Bin/summaryReport.xsl", "$outDir/summaryReport.xsl")
or die "Failed to copy '$FindBin::Bin/summaryReport.xsl' to '$outDir/summaryReport.xsl': $!\n";
}
# Compile per lane statistics
my %laneStat;
my $laneXmlData = {};
my $numLanes = $sisyphus->laneCount();
foreach my $lane (1..$numLanes){
print STDERR "Compiling data for lane $lane\n";
$laneStat{$lane} = undef;
$laneXmlData->{Lane}->{$lane}={};
sumLane(\%dumps, \%laneStat, $lane, \@projects);
if(! defined $laneStat{$lane}){
# A skipped lane without data
my $rId = 0;
foreach my $read ($sisyphus->reads()){
next if($read->{index} eq 'Y');
$rId++;
my %metrics;
$metrics{Id} = $rId;
# Add the RTA/CASAVA metrics
foreach my $key (keys %{$RtaLaneStats->{$lane}->{$rId}}){
$metrics{$key} = $RtaLaneStats->{$lane}->{$rId}->{$key};
}
push @{$laneXmlData->{Lane}->{$lane}->{Read}}, \%metrics;
}
next;
}
my $read = 0;
foreach my $readInfo ($sisyphus->reads()){
next if($readInfo->{index} eq 'Y');
$read++;
my %metrics = ();
if(exists $laneStat{$lane} && exists $laneStat{$lane}->{$read}){
%metrics = $laneStat{$lane}->{$read}->metrics();
# $laneStat{$lane}->{$read}->{METRICS}=\%metrics;
$metrics{Id} = $metrics{Read};
delete($metrics{Read});
delete($metrics{Lane});
}
# Add the RTA/CASAVA metrics
foreach my $key (keys %{$RtaLaneStats->{$lane}->{$read}}){
if($key eq 'Excluded'){
foreach my $eKey (keys %{$RtaLaneStats->{$lane}->{$read}->{Excluded}}){
$metrics{Excluded}->{$eKey} = $RtaLaneStats->{$lane}->{$read}->{Excluded}->{$eKey};
}
}else{
$metrics{$key} = $RtaLaneStats->{$lane}->{$read}->{$key};
}
}
if(exists $laneStat{$lane} && exists $laneStat{$lane}->{$read}){
my $stat = $laneStat{$lane}->{$read};
my $plotTitle = $sisyphus->RUNFOLDER . ', Lane ' . $stat->LANE . ', Read ' . $stat->READ;
my @qplot = $plotter->plotQval($stat,"$outDir/Plots/LanePlots/L00$lane-R$read-Qscores", "Q-score distribution $plotTitle");
($metrics{QscorePlot} = $qplot[0]) =~ s/^$outDir\///;
($metrics{QscorePlotThumb} = $qplot[1]) =~ s/^$outDir\///;
my @aplot = $plotter->plotAdapters($stat,"$outDir/Plots/LanePlots/L00$lane-R$read-Adapters", "Adapter sequences $plotTitle");
($metrics{AdapterPlot} = $aplot[0]) =~ s/^$outDir\///;
($metrics{AdapterPlotThumb} = $aplot[1]) =~ s/^$outDir\///;
my @bplot = $plotter->plotBaseComposition($stat,"$outDir/Plots/LanePlots/L00$lane-R$read-BaseComp", "Base Composition $plotTitle");
($metrics{BaseCompPlot} = $bplot[0]) =~ s/^$outDir\///;
($metrics{BaseCompPlotThumb} = $bplot[1]) =~ s/^$outDir\///;
my @gcplot = $plotter->plotGCdistribution($stat,"$outDir/Plots/LanePlots/L00$lane-R$read-GCdist", "GC Distribution $plotTitle");
($metrics{GCPlot} = $gcplot[0]) =~ s/^$outDir\///;
($metrics{GCPlotThumb} = $gcplot[1]) =~ s/^$outDir\///;
my @lplot = $plotter->plotQ30Length($stat,"$outDir/Plots/LanePlots/L00$lane-R$read-Q30Length", "Q30Length $plotTitle");
($metrics{Q30Plot} = $lplot[0]) =~ s/^$outDir\///;
($metrics{Q30PlotThumb} = $lplot[1]) =~ s/^$outDir\///;
my @dplot = $plotter->plotDuplications($stat,"$outDir/Plots/LanePlots/L00$lane-R$read-Duplicate", "Duplications $plotTitle");
($metrics{DupPlot} = $dplot[0]) =~ s/^$outDir\///;
($metrics{DupPlotThumb} = $dplot[1]) =~ s/^$outDir\///;
my @qpbplot = $plotter->plotQPerBase($stat,"$outDir/Plots/LanePlots/L00$lane-R$read-QvaluePerBase", "Q value per base $plotTitle");
($metrics{QValuePerBase} = $qpbplot[0]) =~ s/^$outDir\///;
($metrics{QValuePerBaseThumb} = $qpbplot[1]) =~ s/^$outDir\///;
}
push @{$laneXmlData->{Lane}->{$lane}->{Read}}, \%metrics;
}
}
print $reportFh $xs->XMLout($laneXmlData, RootName=>'LaneMetrics', KeyAttr => {Lane => 'Id'});
# Compile per project statistics
my $projXmlData;
foreach my $project (@projects){
print STDERR "Compiling data for $project\t";
$projXmlData->{Project}->{$project}={};
# Sum all data for the project
my %lStat;
my %pStat;
my %pCasava;
my %lCasava;
sumProject(\%dumps, \%pStat, \%lStat, \%pCasava, \%lCasava, $project, $RtaSampleStats, $sisyphus);
my @lanes = keys %lStat;
foreach my $read (sort {$a<=>$b} keys %{pStat}){
print STDERR "Read $read\t";
my $stat = $pStat{$read};
my %metrics = $stat->metrics();
die "Could not get any metrics\n" unless(%metrics);
$metrics{Id} = $metrics{Read};
delete($metrics{Read});
delete($metrics{Lane});
# Add the RTA/CASAVA metrics
foreach my $key (keys %{$pCasava{$read}}){
$metrics{$key} = $pCasava{$read}->{$key};
}
my $plotTitle = $sisyphus->RUNFOLDER . ', Project ' . $project . ', Read ' . $stat->READ;
my @qplot = $plotter->plotQval($stat,"$outDir/Plots/ProjPlots/$project-R$read-Qscores", "Q-score distribution $plotTitle");
($metrics{QscorePlot} = $qplot[0]) =~ s/^$outDir\///;
($metrics{QscorePlotThumb} = $qplot[1]) =~ s/^$outDir\///;
my @aplot = $plotter->plotAdapters($stat,"$outDir/Plots/ProjPlots/$project-R$read-Adapters", "Adapter sequences $plotTitle");
($metrics{AdapterPlot} = $aplot[0]) =~ s/^$outDir\///;
($metrics{AdapterPlotThumb} = $aplot[1]) =~ s/^$outDir\///;
my @bplot = $plotter->plotBaseComposition($stat,"$outDir/Plots/ProjPlots/$project-R$read-BaseComp", "Base Composition $plotTitle");
($metrics{BaseCompPlot} = $bplot[0]) =~ s/^$outDir\///;
($metrics{BaseCompPlotThumb} = $bplot[1]) =~ s/^$outDir\///;
my @gcplot = $plotter->plotGCdistribution($stat,"$outDir/Plots/ProjPlots/$project-R$read-GCdist", "GC Distribution $plotTitle");
($metrics{GCPlot} = $gcplot[0]) =~ s/^$outDir\///;
($metrics{GCPlotThumb} = $gcplot[1]) =~ s/^$outDir\///;
my @lplot = $plotter->plotQ30Length($stat,"$outDir/Plots/ProjPlots/$project-R$read-Q30Length", "Q30Length $plotTitle");
($metrics{Q30Plot} = $lplot[0]) =~ s/^$outDir\///;
($metrics{Q30PlotThumb} = $lplot[1]) =~ s/^$outDir\///;
my @dplot = $plotter->plotDuplications($stat,"$outDir/Plots/ProjPlots/$project-R$read-Duplicate", "Duplications $plotTitle");
($metrics{DupPlot} = $dplot[0]) =~ s/^$outDir\///;
($metrics{DupPlotThumb} = $dplot[1]) =~ s/^$outDir\///;
my @qpbplot = $plotter->plotQPerBase($stat,"$outDir/Plots/ProjPlots/$project-R$read-QvaluePerBase", "Q value per base $plotTitle");
($metrics{QValuePerBase} = $qpbplot[0]) =~ s/^$outDir\///;
($metrics{QValuePerBaseThumb} = $qpbplot[1]) =~ s/^$outDir\///;
foreach my $lane (sort{$a<=>$b} @lanes){
next unless(exists $lStat{$lane}); # The undetermined_indices will not have data for lanes where no index was used
my $lstat = $lStat{$lane}->{$read};
my %lmetrics = $lstat->metrics();
die "Could not get any metrics\n" unless(%lmetrics);
$lmetrics{Id} = $lane;
delete($lmetrics{Read});
delete($lmetrics{Lane});
# Add the RTA/CASAVA metrics
foreach my $key (keys %{$lCasava{$lane}->{$read}}){
$lmetrics{$key} = $lCasava{$lane}->{$read}->{$key};
}
my $plotTitle = $sisyphus->RUNFOLDER . ', Project ' . $project . ', LANE ' . $lane . ', Read ' . $lstat->READ;
my @qplot = $plotter->plotQval($lstat,"$outDir/Plots/ProjPlots/Lane$lane/$project-R$read-Qscores", "Q-score distribution $plotTitle");
($lmetrics{QscorePlot} = $qplot[0]) =~ s/^$outDir\///;
($lmetrics{QscorePlotThumb} = $qplot[1]) =~ s/^$outDir\///;
my @aplot = $plotter->plotAdapters($lstat,"$outDir/Plots/ProjPlots/Lane$lane/$project-R$read-Adapters", "Adapter sequences $plotTitle");
($lmetrics{AdapterPlot} = $aplot[0]) =~ s/^$outDir\///;
($lmetrics{AdapterPlotThumb} = $aplot[1]) =~ s/^$outDir\///;
my @bplot = $plotter->plotBaseComposition($lstat,"$outDir/Plots/ProjPlots/Lane$lane/$project-R$read-BaseComp", "Base Composition $plotTitle");
($lmetrics{BaseCompPlot} = $bplot[0]) =~ s/^$outDir\///;
($lmetrics{BaseCompPlotThumb} = $bplot[1]) =~ s/^$outDir\///;
my @gcplot = $plotter->plotGCdistribution($lstat,"$outDir/Plots/ProjPlots/Lane$lane/$project-R$read-GCdist", "GC Distribution $plotTitle");
($lmetrics{GCPlot} = $gcplot[0]) =~ s/^$outDir\///;
($lmetrics{GCPlotThumb} = $gcplot[1]) =~ s/^$outDir\///;
my @lplot = $plotter->plotQ30Length($lstat,"$outDir/Plots/ProjPlots/Lane$lane/$project-R$read-Q30Length", "Q30Length $plotTitle");
($lmetrics{Q30Plot} = $lplot[0]) =~ s/^$outDir\///;
($lmetrics{Q30PlotThumb} = $lplot[1]) =~ s/^$outDir\///;
my @dplot = $plotter->plotDuplications($lstat,"$outDir/Plots/ProjPlots/Lane$lane/$project-R$read-Duplicate", "Duplications $plotTitle");
($lmetrics{DupPlot} = $dplot[0]) =~ s/^$outDir\///;
($lmetrics{DupPlotThumb} = $dplot[1]) =~ s/^$outDir\///;
my @qpbplot = $plotter->plotQPerBase($lstat,"$outDir/Plots/ProjPlots/Lane$lane/$project-R$read-QvaluePerBase", "Q value per base $plotTitle");
($lmetrics{QValuePerBase} = $qpbplot[0]) =~ s/^$outDir\///;
($lmetrics{QValuePerBaseThumb} = $qpbplot[1]) =~ s/^$outDir\///;
push @{$metrics{Lane}}, \%lmetrics;
}
push @{$projXmlData->{Project}->{$project}->{Read}}, \%metrics;
}
print STDERR "\n";
}
print $reportFh $xs->XMLout($projXmlData, RootName=>'ProjectMetrics', KeyAttr => {Project => 'Id', Lane => 'Id'});
# Collect run information
my %metaData;
$metaData{RunFolder} = $sisyphus->runfolder();
$metaData{SisyphusVersion} = $sisyphus->version();
$metaData{CsVersion} = $sisyphus->getCSversion();
$metaData{InstrumentModel} = $sisyphus->machineType();
$metaData{RtaVersion} = $sisyphus->getRTAversion();
$metaData{FlowCellId} = $sisyphus->fcId();
$metaData{ClusterKitVersion} = $sisyphus->getClusterKitVersion();
$metaData{Qoffset} = $offset;
# metaData information only available for HiSeq
if($sisyphus->machineType() ne 'miseq'){
$metaData{FlowCellVer} = $sisyphus->getFlowCellVersion();
$metaData{SBSversion} = $sisyphus->getSBSversion();
}
my $runInfo = $sisyphus->getRunInfo();
for(my $i=0; $i<@{$runInfo->{reads}}; $i++){
my $read = $runInfo->{reads}->[$i];
my $cycles = $read->{last} - $read->{first}; # The last cycle is discarded, so do not add one here
if($runInfo->{indexed}){
if($read->{id} != 2){
my $id = $read->{id};
if($runInfo->{indexed} && $id>1){ # Do not count the index read
$id -= 1;
}
push @{$metaData{Read}}, {Id=>$id, Cycles=>$cycles};
}
}else{
push @{$metaData{Read}}, {Id=>$read->{id}, Cycles=>$cycles};
}
}
print $reportFh $xs->XMLout(\%metaData, RootName=>'MetaData');
print $reportFh q(</SummaryReport>), "\n";
close($reportFh);
# Transform xml + xsl to html
my $xslt = XML::LibXSLT->new();
my $stylesheet = $xslt->parse_stylesheet(XML::LibXML->load_xml(location=>"$outDir/summaryReport.xsl", no_cdata=>1));
my $xmlData = stripXmlNameSpace("$outDir/summaryReport.xml");
open(my $htmlFh, '>', "$outDir/summaryReport.html") or die "Failed to open '$outDir/summaryReport.xsl' for writing: $!\n";
print $htmlFh
$stylesheet->output_as_bytes(
$stylesheet->transform(
XML::LibXML->load_xml(
string => $xmlData
)
)
);
close($htmlFh);
# Link project reports
foreach my $project (@projects){
linkDir("$rfPath/Statistics/Project_$project", "$outDir/$project");
}
# Calculate checksums for all summary files
$sisyphus->md5Dir($outDir, -noCache=>1, -save=>1);
sub linkDir{
my $srcDir = shift;
my $targetDir = shift;
unless(-e $targetDir){
mkpath($targetDir, 2770) or die "Failed to create path '$targetDir': $!\n";
}
opendir(my $sdFh, $srcDir) or die "Failed to open dir '$srcDir': $!\n";
while(my $file = readdir($sdFh)){
next if($file=~m/^\.+$/);
next if($file=~m/\.fastq(\.gz)?$/);
next if($file eq 'extractProject.complete');
if(-d "$srcDir/$file"){
linkDir("$srcDir/$file", "$targetDir/$file");
}else{
link("$srcDir/$file", "$targetDir/$file") || die qq(Failed to link "$srcDir/$file", "$targetDir/$file": $!\n);
}
}
closedir($sdFh);
}
sub sumLane{
my $dumps = shift;
my $laneStat = shift;
my $lane = shift;
my $projects = shift;
my %samples;
foreach my $proj (@{$projects}){
if(exists $dumps->{$proj}->{$lane} && ref $dumps->{$proj}->{$lane} eq 'ARRAY'){
foreach my $stat (@{$dumps->{$proj}->{$lane}}){
my $read = $stat->{READ};
if(defined $laneStat->{$lane}->{$read}){
my $tmp = $laneStat->{$lane}->{$read}->add($stat);
$laneStat->{$lane}->{$read} = $tmp;
}else{
$laneStat->{$lane}->{$read} = $stat->copy;
}
$samples{$read}++ unless($proj =~ /undetermined_indices/i);
}
}
}
foreach my $r (keys %samples){
if(exists $laneStat->{$lane}->{$r}){
$laneStat->{$lane}->{$r}->{SamplesOnLane} = $samples{$r};
}
}
}
sub sumProject{
my $dumps = shift;
my $pStat = shift;
my $lStat = shift;
my $pCasava = shift;
my $lCasava = shift;
my $proj = shift;
my $rtaStat = shift;
foreach my $l (keys %{$dumps->{$proj}}){
my $laneData = $dumps->{$proj}->{$l};
foreach my $stat (@{$laneData}){
my $read = $stat->READ;
if(defined $pStat->{$read}){
my $tmp = $pStat->{$read}->add($stat);
$pStat->{$read} = $tmp;
}else{
$pStat->{$read} = $stat->copy;
}
if(defined $lStat->{$l}->{$read}){
my $tmp = $lStat->{$l}->{$read}->add($stat);
$lStat->{$l}->{$read} = $tmp;
}else{
$lStat->{$l}->{$read} = $stat->copy;
}
unless(exists $pCasava->{$read}){
$pCasava->{$read} = {
QscoreSum => 0,
PctLane => 0,
YieldPF => 0,
PF => 0,
mismatchCnt1 => 0,
AvgQ => 0,
YieldQ30 => 0,
PctQ30 => 0,
TagErr => 0
};
}
unless(exists $lCasava->{$l}->{$read}){
$lCasava->{$l}->{$read} = {
QscoreSum => 0,
PctLane => 0,
YieldPF => 0,
PF => 0,
mismatchCnt1 => 0,
AvgQ => 0,
YieldQ30 => 0,
PctQ30 => 0,
TagErr => 0
};
}
my $casava = $pCasava->{$read};
my $lcasava = $lCasava->{$l}->{$read};
my $sample = $stat->SAMPLE;
unless($l == $stat->LANE){die "Incongruent lanes!"};
my $tag = $stat->TAG;
if( ((! defined $tag) || $tag eq '') &&
(! defined $rtaStat->{$sample}->{$l}->{$read}->{$tag}) &&
defined $rtaStat->{$sample}->{$l}->{$read}->{'NoIndex'}){
$tag = 'NoIndex';
}
$tag = $sisyphus->getIndexUsingSampleNumber($l, $proj, $sample, substr($tag,1), $sampleSheet) if($machineType eq "hiseqx" && $proj ne 'Undetermined_indices');
my $data;
if($proj eq 'Undetermined_indices'){
if($machineType eq "hiseqx"){
$data = $rtaStat->{unknown}->{$l}->{$read}->{unknown};
}
else{
$data = $rtaStat->{$sample}->{$l}->{$read}->{Undetermined};
}
}else{
$data = $rtaStat->{$sample}->{$l}->{$read}->{$tag};
}
if(defined $data){
# The averages must be updated before the counts are added
if($casava->{YieldPF} + $data->{YieldPF} > 0){
$casava->{AvgQ} = ($casava->{AvgQ}*$casava->{YieldPF} + $data->{AvgQ} * $data->{YieldPF}) / ($casava->{YieldPF} + $data->{YieldPF});
$casava->{PctQ30} = ($casava->{PctQ30}*$casava->{YieldPF} + $data->{YieldPF} * $data->{PctQ30}) / ($casava->{YieldPF} + $data->{YieldPF});
$casava->{TagErr} = ($casava->{TagErr}*$casava->{YieldPF} + $data->{TagErr} * $data->{YieldPF}) / ($casava->{YieldPF} + $data->{YieldPF});
}else{
$casava->{AvgQ} = 0;
$casava->{PctQ30} = 0;
$casava->{TagErr} = 0;
}
foreach my $key ( qw(QscoreSum PF YieldPF mismatchCnt1 YieldQ30 PctLane) ){
$casava->{$key} += $data->{$key};
}
if($lcasava->{YieldPF} + $data->{YieldPF} > 0){
$lcasava->{AvgQ} = ($lcasava->{AvgQ}*$lcasava->{YieldPF} + $data->{AvgQ} * $data->{YieldPF}) / ($lcasava->{YieldPF} + $data->{YieldPF});
$lcasava->{PctQ30} = ($lcasava->{PctQ30}*$lcasava->{YieldPF} + $data->{YieldPF} * $data->{PctQ30}) / ($lcasava->{YieldPF} + $data->{YieldPF});
$lcasava->{TagErr} = ($lcasava->{TagErr}*$lcasava->{YieldPF} + $data->{TagErr} * $data->{YieldPF}) / ($lcasava->{YieldPF} + $data->{YieldPF});
}else{
$lcasava->{AvgQ} = 0;
$lcasava->{PctQ30} = 0;
$lcasava->{TagErr} = 0;
}
foreach my $key ( qw(QscoreSum PF YieldPF mismatchCnt1 YieldQ30 PctLane) ){
$lcasava->{$key} += $data->{$key};
}
}else{
warn "No casava data found for:\n SAMPLE: $sample\n LANE: $l\n TAG: $tag\n";
}
}
}
}
sub stripXmlNameSpace{
my $file = shift;
# Strip the namespace from the xml data, adding it to the xsl is a mess
$/='';
open(my $xmlFh, $file) or die;
my $xmlData = <$xmlFh>;
close($xmlFh);
$/="\n";
$xmlData=~ s/xmlns="illuminareport.xml.molmed"//;
return $xmlData;
}