-
Notifications
You must be signed in to change notification settings - Fork 10
/
quickReport.pl
executable file
·290 lines (240 loc) · 10.1 KB
/
quickReport.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
#!/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 File::Basename;
use Molmed::Sisyphus::Common qw(mkpath);
use Molmed::Sisyphus::QStat;
=pod
=head1 NAME
quickReport.pl - Create a brief report and mail it to the specified address
=head1 SYNOPSIS
quickReport.pl -help|-man
quickReport.pl -runfolder <runfolder> -mail <email address> [-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 -mail
Send the report to this email address
=item -debug
Print debugging information
=back
=head1 DESCRIPTION
Compiles some quick statistics about the run and sends a mail to the specified address.
=cut
# Parse options
my($help,$man) = (0,0);
my($rfPath,$mail) = (undef,undef);
my $sender = '[email protected]';
our($debug) = 0;
GetOptions('help|?'=>\$help,
'man'=>\$man,
'runfolder=s' => \$rfPath,
'mail=s' => \$mail,
'sender=s' => \$sender,
'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;
}
my $OFFSET = 33;
# Create a new sisyphus object for common functions
my $sisyphus = Molmed::Sisyphus::Common->new(PATH=>$rfPath, DEBUG=>$debug);
$rfPath = $sisyphus->PATH;
my $ignoreFastqs = 0;
my $config = $sisyphus->readConfig();
if(defined($config->{IGNORE_MISSING_FASTQS})){
$ignoreFastqs = $config->{IGNORE_MISSING_FASTQS};
}
# Initialize the random generator and use a random seed based upon the flowcell id
my $fcid = $sisyphus->fcId() || "ABC123CXX";
my $rseed = 0;
foreach my $c (split //, $fcid) {
$rseed += ord($c);
}
srand($rseed);
print STDERR "Using random seed $rseed based on fcId $fcid\n";
# Get the statistics generated by RTA/CASAVA
my($RtaLaneStats,$RtaSampleStats) = $sisyphus->resultStats();
my $sampleSheet = $sisyphus->readSampleSheet();
my $machineType = $sisyphus->machineType();
my %files;
my $baseQC;
sub findFastq{
my $files = shift;
my $file = $_;
if($file !~ /Undetermined_indices/ && $file !~ /\/Data\// && $file !~ /\/IndexCheck\//){
if($file =~ m/\.fastq(\.gz)?$/){
my @path = split '/', $file;
my $project = $path[-3];
$project =~ s/^Project_//;
my $sample = $path[-2];
$sample =~ s/^Sample_//;
if($file =~ m/.*\/(.+)_([ACTG]+-?[ACGT]*|NoIndex)_L(\d{3})_R(\d)_(\d{3})\.fastq\.gz$/){
my ($sample,$index,$laneId,$read,$segment) = ($1,$2,$3,$4,$5);
$index =~ s/NoIndex/Undetermined/ if($index =~ /NoIndex/);
$files->{$laneId}{$sample}{$index}{$read}{$segment} = $file;
}
elsif($file =~ m/.*\/(.+)_S(\d*)_L(\d{3})_R(\d)_(\d{3})\.fastq\.gz$/){
my ($sample,$index,$laneId,$read,$segment) = ($1,$2,$3,$4,$5);
$sample = "Sample_" . $sample;
$files->{$laneId}{$sample}{$index}{$read}{$segment} = $file;
}
}
}
}
use File::Find;
find({wanted => sub{findFastq(\%files)}, no_chdir => 1, follow => 1}, "$rfPath/Unaligned");
my $laneQC;
my $samples;
foreach my $proj (keys %{$sampleSheet}){
foreach my $lid (keys %{$sampleSheet->{$proj}}){
foreach my $tag (keys %{$sampleSheet->{$proj}->{$lid}}){
my $info = $sampleSheet->{$proj}->{$lid}->{$tag};
my $laneId = ("0" x (3 - length($lid))) . $lid;
my $fastQFilesFound = 0;
my $indexOrSampleCounter = $info->{SampleNumber};
foreach my $read (keys %{$files{$laneId}{$info->{SampleID}}{$indexOrSampleCounter}}){
foreach my $pctLane (keys %{$files{$laneId}{$info->{SampleID}}{$indexOrSampleCounter}{$read}}){
my $stat = Molmed::Sisyphus::QStat->new(OFFSET=>$OFFSET, DEBUG=>$debug);
my $filehandle;
$samples->{$info->{SampleName}}->{$lid}->{$tag} = 1;
if($files{$laneId}{$info->{SampleID}}{$indexOrSampleCounter}{$read}{$pctLane} =~ /fastq.gz$/) {
open($filehandle, "zcat $files{$laneId}{$info->{SampleID}}{$indexOrSampleCounter}{$read}{$pctLane} |") or die "Failed to open $files{$laneId}{$info->{SampleID}}{$indexOrSampleCounter}{$read}{$pctLane}: $!";
} elsif($files{$laneId}{$info->{SampleID}}{$indexOrSampleCounter}{$read}{$pctLane} =~ /fastq$/) {
open($filehandle,'-|', "grep fastq.gz $files{$laneId}{$info->{SampleID}}{$indexOrSampleCounter}{$read}{$pctLane}") or die "Failed to open $files{$laneId}{$info->{SampleID}}{$indexOrSampleCounter}{$read}{$pctLane}: $!";
}
$fastQFilesFound = 1;
my $seq = "";
my $qual = "";
my $counter = 0;
FASTQ: while(<$filehandle>) {
$seq = <$filehandle>;
chomp($seq);
<$filehandle>;
$qual = <$filehandle>;
chomp($qual);
if(rand() < $stat->{SAMPLING_DENSITY}) {
$counter = $counter + 1;
$stat->addQValuePerBaseAndPosition($seq,$qual);
last FASTQ if($counter > $stat->{SAMPLING_COUNTER});
}
}
close($filehandle);
$baseQC->{$info->{SampleID}}->{$lid}->{$read}->{$indexOrSampleCounter} = $stat->calculateQValuePerBase();
if(!defined($laneQC->{$lid}->{$read})) {
$laneQC->{$lid}->{$read} = Molmed::Sisyphus::QStat->new(OFFSET=>$OFFSET, DEBUG=>$debug);
}
$laneQC->{$lid}->{$read} = $laneQC->{$lid}->{$read}->add($stat);
}
}
if($fastQFilesFound == 0) {
print "Couldn't find fastq files(s) for lane $laneId, $info->{SampleID}\n";
unless($ignoreFastqs){
exit 1;
}
}
}
}
}
my %laneFrac;
my %laneUnknown;
my $reads;
foreach my $sample (keys %{$RtaSampleStats}){
foreach my $lane (keys %{$RtaSampleStats->{$sample}}){
foreach my $barcode (keys %{$RtaSampleStats->{$sample}->{$lane}->{1}}){
if($barcode eq 'Undetermined' || ($barcode eq 'unknown' && $sample eq 'Undetermined')){
$laneUnknown{$lane} = sprintf('%.1f', $RtaSampleStats->{$sample}->{$lane}->{1}->{$barcode}->{PctLane});
}
else{
if(defined($samples->{$sample}->{$lane}->{$barcode})) {
push @{$laneFrac{$lane}}, sprintf(' %.1f:%s', $RtaSampleStats->{$sample}->{$lane}->{1}->{$barcode}->{PctLane}, $sample);
}
}
}
}
}
open(my $repFh, '>', "$rfPath/quickReport.txt");
print $repFh join("\t", "Lane", "Read", "ReadsPF (M)", "Yield Q30 (G)", "ErrRate", "Excluded", "Q per base (A/C/G/T)", "Sample Fractions", "Unidentified"), "\n";
foreach my $lane (sort {$a<=>$b} keys %{$RtaLaneStats}){
foreach my $read (sort {$a<=>$b} keys %{$RtaLaneStats->{$lane}}){
print $repFh join("\t", $lane, $read,
sprintf('%.0f', (defined($RtaLaneStats->{$lane}->{$read}->{PF}) ? $RtaLaneStats->{$lane}->{$read}->{PF} : 0)/1e6),
sprintf('%.1f', (defined($RtaLaneStats->{$lane}->{$read}->{YieldQ30}) ? $RtaLaneStats->{$lane}->{$read}->{YieldQ30} : 0)/1e9),
defined($RtaLaneStats->{$lane}->{$read}->{ErrRate}) ? $RtaLaneStats->{$lane}->{$read}->{ErrRate} : '-',
$RtaLaneStats->{$lane}->{$read}->{ExcludedTiles});
my @fractionSorted = defined($laneFrac{$lane}) ? sort({sortLaneFrac($a,$b)} @{$laneFrac{$lane}}) : ('-');
if(defined($laneQC->{$lane}) && defined($laneQC->{$lane}->{$read})) {
my $result = $laneQC->{$lane}->{$read}->calculateQValuePerBase();
print $repFh "\t";
printf $repFh sprintf('%.1f±%.1f,', $result->{"a"}->{MEAN},$result->{'a'}->{STDV});
printf $repFh sprintf('%.1f±%.1f,', $result->{"c"}->{MEAN},$result->{'c'}->{STDV});
printf $repFh sprintf('%.1f±%.1f,', $result->{"g"}->{MEAN},$result->{"g"}->{STDV});
printf $repFh sprintf('%.1f±%.1f', $result->{"t"}->{MEAN},$result->{'t'}->{STDV});
}else {
print $repFh "\t-,-,-,-";
}
print $repFh "\t", join(',', @fractionSorted);
print $repFh "\t", (defined($laneUnknown{$lane}) ? $laneUnknown{$lane} : 0);
print $repFh "\n";
}
}
close($repFh);
if(defined $mail && $mail =~ m/\w\@\w/){
open(my $repFh, '<', "$rfPath/quickReport.txt");
my $msg = '<html><body><table>' . "\n";
my $i=0;
while(<$repFh>){
$i++;
s:\t:</td><td>:g;
s:,:<br />:g;
if($i==1){
s/td/th/g;
$msg .= '<tr><th>' . $_ . '</tr>';
}elsif($i%2 > 0){
$msg .= '<tr><td>' . $_ . '</tr>';
}else{
$msg .= '<tr bgcolor="#dddddd"><td>' . $_ . '</tr>';
}
}
$msg .= '</table>'. "\n";
$msg .= '</body></html>';
require Net::SMTP;
#Create a new object with 'new'.
my $smtp = Net::SMTP->new("smtp.uu.se");
#Send the MAIL command to the server.
$smtp->mail($sender);
#Send the server the 'Mail To' address.
$smtp->to($mail);
#Start the message.
$smtp->data();
#Send the message.
$smtp->datasend("From: $sender\n");
$smtp->datasend("To: $mail\n");
$smtp->datasend("Subject: [Sisyphus] [Quick Report]" . basename($rfPath) . "\n");
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-Type: text/html; charset=us-ascii\n");
$smtp->datasend("\n");
$smtp->datasend("$msg\n\n");
#End the message.
$smtp->dataend();
#Close the connection to your server.
$smtp->quit();
}
sub sortLaneFrac{
my @a = split ':', $_[0];
my @b = split ':', $_[1];
return($a[0]<=>$b[0]);
}