-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathqc3.pl
331 lines (303 loc) · 9.97 KB
/
qc3.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
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
use File::Basename;
use Getopt::Long;
use FindBin;
use lib $FindBin::Bin;
use HTML::Template;
use source::makeReport;
use source::fastqSummary;
use source::bamSummary;
use source::vcfSummary;
our $version = "1.34";
my $qc3ConfigFile = dirname($0) . "/config.txt";
my %config;
my $usageModule = "
Program: qc3.pl (a quality control tool for DNA sequencing data in raw data, alignment, and variant calling stages)
Version: $version
Usage: perl qc3.pl -m module -i listfile -o outputDirectory [-t threads] [other options]
Options:
-m module Required. QC module used. It should be f (fastq QC), b (bam QC), or v (vcf QC).
-i input filelist Required. Input file. In fastq QC or bam QC, it should be a file listing all analyzed files (supports .fastq, .fastq.gz, and .bam files). To analyze the pair-end fastq files in fastq QC, the two files for the same sample should be listed together in this file. In vcf QC, it should be a vcf file.
-o output directory Required. Output directory for QC result. If the directory doesn't exist, it would be created.
-t threads Optional. Threads used in analysis. The default value is 4. This parameter only valid for fastq and bam QC. Only one thread will be used in vcf QC.
-h help Optional. Show this information.
To see help information for each module, please use -m parameter to specify module.
For more information, please refer to the readme file in QC3 directory. Or visit the QC3 website at https://github.com/slzhao/QC3
";
$| = 1;
my $commandline = "perl $0 "
. join( " ", @ARGV )
; #Output you input command in the report to reproduce you result
my (
$module, $filelist, $resultDir, $singleEnd,
$targetregionfile, $gtffile, $isdepth, $nod,
$caculateMethod, $vcfCfgFile, $method, $maxThreads,
$annovarDb, $usePASS, $xym, $rePlot,
$no_batch, $use_SM, $d_cumul, $showHelp
) = ();
our @log : shared;
GetOptions(
"t=i" => \$maxThreads,
"m=s" => \$module,
"i=s" => \$filelist,
"o=s" => \$resultDir,
"se" => \$singleEnd,
"r=s" => \$targetregionfile,
"g=s" => \$gtffile,
"d" => \$isdepth,
"nod" => \$nod,
"d_cumul=s" => \$d_cumul,
"no_batch" => \$no_batch,
"use_SM" => \$use_SM,
"cm=i" => \$caculateMethod,
"c:s" => \$vcfCfgFile,
"s=i" => \$method,
"a=s" => \$annovarDb,
"up" => \$usePASS,
"xym" => \$xym,
"rp" => \$rePlot,
"h" => \$showHelp,
);
if ( defined $module and $showHelp ) {
$config{'showHelp'} = $showHelp;
if ( $module eq "f" ) {
my $rResult = &fastqSummary( $filelist, \%config );
}
elsif ( $module eq "b" ) {
my $rResult = &bamSummary( $filelist, \%config );
}
elsif ( $module eq "v" ) {
my $rResult = &vcfSummary( $filelist, \%config );
}
}
elsif ($showHelp) { die "$usageModule"; }
open QC3CFG, "<$qc3ConfigFile" or die "Can't read $qc3ConfigFile\n$!";
while (<QC3CFG>) {
chomp;
if (/^#/) {
next;
}
my @lines = ( split /[="]/, $_ );
$config{ $lines[0] } = $lines[2];
}
my $detectR = `which $config{"RBin"}`;
if ( $detectR eq '' ) {
die
"Can't find R. Please install R or modify the RBin in config.txt.\n$usageModule";
}
if ( !defined $module
or ( $module ne 'f' and $module ne 'b' and $module ne 'v' ) )
{
die(
"Module (-m) is required and must be f (fastq), b (bam), and v (vcf)\n$usageModule"
);
}
elsif ( !defined $filelist or !defined $resultDir ) {
die(
"Input file (-i) and Output directory (-o) must be provided\nYou can use '$commandline -h' to see help information for the module\n$usageModule"
);
}
elsif ( !( -s $filelist ) ) {
die(
"Input file (-i) didn't exist or size equal 0\nYou can use '$commandline -h' to see help information for the module\n$usageModule"
);
}
if ( !defined $targetregionfile and defined $nod ) {
die(
"Target region file (-r) must be provided if option -nod used.\nYou can use '$commandline -h' to see help information for the module\n$usageModule"
);
}
if ( !defined $isdepth ) { $isdepth = 0; }
if ( !defined $nod ) { $nod = 0; }
if ( !defined $d_cumul ) { $d_cumul = "0,10,30"; }
my @d_cumul_array = split(',', $d_cumul);
if ( !defined $no_batch ) { $no_batch = 0; }
if ( !defined $use_SM ) { $use_SM = 0; }
if ( !defined $usePASS ) { $usePASS = 0; }
if ( !defined $xym ) { $xym = 0; }
if ( !defined $method ) { $method = 1; }
if ( !( defined $vcfCfgFile ) or $vcfCfgFile eq '' ) {
$vcfCfgFile = dirname($0) . '/GATK.cfg';
}
if ( !defined $maxThreads ) { $config{'maxThreads'} = 4; }
else {
$config{'maxThreads'} = $maxThreads;
}
if ( !defined $singleEnd ) {
$config{'singleEnd'} = 0;
}
else {
$config{'singleEnd'} = $singleEnd;
}
if ( !defined $caculateMethod ) {
$config{'caculateMethod'} = 1;
} #1 as mean 2 as median
else {
$config{'caculateMethod'} = $caculateMethod;
}
$config{'resultDir'} = $resultDir;
if ( !defined $rePlot ) {
$config{'rePlot'} = 0;
}
else {
$config{'rePlot'} = $rePlot;
}
if ( !( -e $resultDir ) ) {
if ( mkdir $resultDir ) {
}
else {
die "can't make result dir. $!\n";
}
}
my $reportHash;
${$reportHash}{'COMMAND'} = $commandline;
${$reportHash}{'CREATTIME'} = localtime;
my $template;
my $reportFileName;
if ( defined $resultDir ) {
open LOG, ">$resultDir/" . $config{'logFileName'}
or die "can't open log file. $!\n";
}
if ( $module eq "f" ) {
pInfo( "Start fastq summary for $filelist", \@log );
my $rResult = &fastqSummary( $filelist, \%config );
if ( $rResult != 0 ) {
pInfo(
"Something wrong in plotting figure. Please check the fastqSummary.rLog file!",
\@log
);
}
#report
my $figureList1 =
&dir2list( $resultDir, "/fastqFigure/", "^batch_", "FIGUREBATCH" );
my $table1 =
&file2table( "$resultDir/fastqResult/fastqSummary.txt", '', 1 );
${$reportHash}{'FIGUREBASESCOREYN'} = "./fastqFigure/fastqScoreYN.png";
${$reportHash}{'FIGUREBASESCOREYNLEGEND'} =
"./fastqFigure/fastqScoreYN.png.legend.png";
${$reportHash}{'FIGUREBASENUCYN'} = "./fastqFigure/nucPercentyYN.png";
if ( defined($figureList1) ) {
${$reportHash}{'FIGURELOOP1'} = $figureList1;
};
${$reportHash}{'MAKETABLE1'} = $table1;
$template =
&build_template( dirname($0) . "/template/report_tmpl_fastq.tmpl",
$reportHash );
$reportFileName = "fastqReport.html";
}
elsif ( $module eq "b" ) {
pInfo( "Start bam summary for $filelist", \@log );
$config{'targetregionfile'} = $targetregionfile;
$config{'gtffile'} = $gtffile;
$config{'isdepth'} = $isdepth;
$config{'nod'} = $nod;
$config{'d_cumul1'} = $d_cumul_array[0];
$config{'d_cumul2'} = $d_cumul_array[1];
$config{'d_cumul3'} = $d_cumul_array[2];
$config{'no_batch'} = $no_batch;
$config{'use_SM'} = $use_SM;
my $rResult = &bamSummary( $filelist, \%config );
if ( $rResult != 0 ) {
pInfo(
"Something wrong in plotting figure. Please check the bamSummary.rLog file!",
\@log
);
}
#report
my $figureList1 =
&dir2list( $resultDir, "/bamFigure/", "^summary_lines", "FIGUREDIS" );
${$reportHash}{'FIGURELOOP1'} = $figureList1;
my $figureList2 =
&dir2list( $resultDir, "/bamFigure/", "^batch_", "FIGUREBATCH" );
if ( defined($figureList2) ) {
${$reportHash}{'FIGURELOOP2'} = $figureList2;
};
my $table1 = &file2table( "$resultDir/bamResult/bamSummary.txt", '', 1 );
${$reportHash}{'MAKETABLE1'} = $table1;
$template =
&build_template( dirname($0) . "/template/report_tmpl_bam.tmpl",
$reportHash );
$reportFileName = "bamReport.html";
}
elsif ( $module eq "v" ) {
pInfo( "Start vcf summary for $filelist", \@log );
$config{'vcfCfgFile'} = $vcfCfgFile;
$config{'method'} = $method;
$config{'annovarDb'} = $annovarDb;
$config{'usePASS'} =$usePASS;
$config{'xym'} = $xym;
my $rResult = &vcfSummary( $filelist, \%config );
if ( $rResult != 0 ) {
pInfo(
"Something wrong in plotting figure. Please check the vcfSummary.rLog file!",
\@log
);
}
#report
my $vcfFileName = basename($filelist);
my $cfgFileContent = &file2text($vcfCfgFile);
my $table1 =
&file2table( "$resultDir/vcfResult/$vcfFileName.SampleNumber.txt", '',
1 );
my $table2 =
&file2table( "$resultDir/vcfResult/$vcfFileName.Method$method.txt",
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ], 1 );
my $table3 =
&file2table( "$resultDir/vcfResult/$vcfFileName.snpCount.txt", '', 1 );
my $table4;
if (
-e "$resultDir/vcfAnnovarResult/$vcfFileName.pass.avinput.annovar.countTable.txt"
)
{
$table4 = &file2table(
"$resultDir/vcfAnnovarResult/$vcfFileName.pass.avinput.annovar.countTable.txt",
'', 1
);
}
my $table5 =
&file2table( "$resultDir/vcfResult/$vcfFileName.sexCheck.txt", '', 1 );
my $table6 =
&file2table( "$resultDir/vcfResult/$vcfFileName.snpNucCount.txt", '', 1 );
my $figureList1 =
&dir2list( $resultDir, "/vcfFigure/", "scoreCompare", "FIGURE2" );
my $figureList2 =
&dir2list( $resultDir, "/vcfAnnovarResult/", ".variant_function", "FS2",
1 );
${$reportHash}{'MAKETABLE1'} = $table1;
${$reportHash}{'MAKETABLE2'} = $table2;
${$reportHash}{'MAKETABLE3'} = $table3;
${$reportHash}{'MAKETABLE4'} = $table4;
${$reportHash}{'MAKETABLE5'} = $table5;
${$reportHash}{'MAKETABLE6'} = $table6;
${$reportHash}{'FILTERFILE'} = $vcfCfgFile;
${$reportHash}{'FILTERFILECONTENT'} = $cfgFileContent;
${$reportHash}{'FIG'} = "./vcfFigure/$vcfFileName.Method$method.txt.png";
${$reportHash}{'FIGNUMBER'} = "./vcfFigure/$vcfFileName.sampleNumber.png";
${$reportHash}{'FIGURE1'} = $figureList1;
if ( defined($figureList2) ) {
${$reportHash}{'FL1'} = $figureList2;
}
$template =
&build_template( dirname($0) . "/template/report_tmpl_vcf.tmpl",
$reportHash );
$reportFileName = "vcfReport.html";
}
else {
die
"Module (-m) is required and must be f (fastq), b (bam), and v (vcf)\n$usageModule";
}
open REPORT, ">$resultDir/$reportFileName" or die "can't open $!\n";
print REPORT $template->output;
pInfo( "Success!", \@log );
foreach my $log (@log) {
print LOG $log;
}
sub pInfo {
my $s = shift;
print "[", scalar(localtime), "] $s\n";
push @{ $_[1] }, "[" . scalar(localtime) . "] $s\n";
}