This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
m6A-SAC-seq_6.pl
625 lines (501 loc) · 38.6 KB
/
m6A-SAC-seq_6.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
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
#!/usr/bin/env perl
use strict;
use warnings;
use v5.12;
## Perl5 version >= 5.12
###################################################################################################################################################################################################
###################################################################################################################################################################################################
my $genome_g = ''; ## such as "mm10", "ce11", "hg38".
my $input_g = ''; ## such as "5-rawBAM/3_STAR"
my $output_g = ''; ## such as "6-finalBAM/3_STAR"
{
## Help Infromation
my $HELP = '
------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------
Step 6: Only the mapped reads with MAPQ>0 are retained and reads on other chromosomes, all of them are removed.
And assess the quality of BAM files to identify possible mapping errors or biases by using 14 softwares:
SAMtools, Subread utilities, FASTQC, SAMstat, qualimap, deepTools, PRESEQ, Picard, goleft, Bamtools, QoRTs, RSeQC, RNA-SeQC, and MultiQC.
If this script works well, you do not need to check the the versions of the softwares or packages whcih are used in this script.
And you do not need to exactly match the versions of the softwares or packages.
If some errors or warnings are reported, please check the versions of softwares or packages.
The versions of softwares or packages are used in this script:
Perl, 5.26.1
Kallisto, 0.46.1
Salmon, v1.1.0
STAR, 2.7.3a
HISAT2, 2.1.0
Subjunc, 2.0.0
GSNAP, 2019-09-12
SAMtools, 1.10
Subread, 2.0.0
FASTQC, v0.11.9
SAMstat, 1.5.1
qualimap, v.2.2.2-dev
deepTools, 3.3.2
PRESEQ, 2.0.3
Picard, 2.21.6
goleft, 0.2.1
Bamtools, 2.5.1
QoRTs, 1.3.6
RSeQC, 3.0.1 ##such as tin.py, geneBody_coverage.py
RNA-SeQC, 2.3.5 (rnaseqc)
MultiQC, 1.9
Usage:
perl m6A-SAC-seq_6.pl [-version] [-help] [-genome RefGenome] [-in inputDir] [-out outDir] [-mis mismatch]
For instance:
perl m6A-SAC-seq_6.pl -genome hg38 -in 5-rawBAM/3_STAR -out 6-finalBAM/3_STAR > m6A-SAC-seq_6.runLog
----------------------------------------------------------------------------------------------------------
Optional arguments:
-version Show version number of this program and exit.
-help Show this help message and exit.
Required arguments:
-genome RefGenome "RefGenome" is the short name of your reference genome, such as "mm10", "ce11", "hg38". (no default)
-in inputDir "inputDir" is the name of input path that contains your FASTQ files. (no default)
-out outDir "outDir" is the name of output path that contains your running results (BAM files) of this step. (no default)
-----------------------------------------------------------------------------------------------------------
For more details about this pipeline and other NGS data analysis piplines, please visit https://github.com/CTLife/2ndGS_Pipelines
------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------
';
## Version Infromation
my $version = " The 6th Step, version 1.4, 2022-08-05";
## Keys and Values
if ($#ARGV == -1) { say "\n$HELP\n"; exit 0; } ## when there are no any command argumants.
if ($#ARGV%2 == 0) { @ARGV = (@ARGV, "-help") ; } ## when the number of command argumants is odd.
my %args = @ARGV;
## Initialize Variables
$genome_g = 'hg38'; ## This is only an initialization value or suggesting value, not default value.
$input_g = '5-rawBAM/3_STAR'; ## This is only an initialization value or suggesting value, not default value.
$output_g = '6-finalBAM/3_STAR'; ## This is only an initialization value or suggesting value, not default value.
## Available Arguments
my $available = " -version -help -genome -in -out -mis ";
my $boole = 0;
while( my ($key, $value) = each %args ) {
if ( ($key =~ m/^\-/) and ($available !~ m/\s$key\s/) ) {say "\n\tCann't recognize $key"; $boole = 1; }
}
if($boole == 1) {
say "\tThe Command Line Arguments are wrong!";
say "\tPlease see help message by using 'perl m6A-SAC-seq_6.pl -help' \n";
exit 0;
}
## Get Arguments
if ( exists $args{'-version' } ) { say "\n$version\n"; exit 0; }
if ( exists $args{'-help' } ) { say "\n$HELP\n"; exit 0; }
if ( exists $args{'-genome' } ) { $genome_g = $args{'-genome' }; }else{say "\n -genome is required.\n"; say "\n$HELP\n"; exit 0; }
if ( exists $args{'-in' } ) { $input_g = $args{'-in' }; }else{say "\n -in is required.\n"; say "\n$HELP\n"; exit 0; }
if ( exists $args{'-out' } ) { $output_g = $args{'-out' }; }else{say "\n -out is required.\n"; say "\n$HELP\n"; exit 0; }
## Conditions
$genome_g =~ m/^\S+$/ || die "\n\n$HELP\n\n";
$input_g =~ m/^\S+$/ || die "\n\n$HELP\n\n";
$output_g =~ m/^\S+$/ || die "\n\n$HELP\n\n";
## Print Command Arguments to Standard Output
say "\n
################ Arguments ###############################
Reference Genome: $genome_g
Input Path: $input_g
Output Path: $output_g
##########################################################
\n";
}
###################################################################################################################################################################################################
###################################################################################################################################################################################################
say "\n\n\n\n\n\n##################################################################################################";
say "Running......";
sub myMakeDir {
my $path = $_[0];
if ( !( -e $path) ) { system("mkdir -p $path"); }
if ( !( -e $path) ) { mkdir $path || die; }
}
my $output2_g = "$output_g/QC_Results";
&myMakeDir($output_g);
&myMakeDir($output2_g);
opendir(my $DH_input_g, $input_g) || die;
my @inputFiles_g = readdir($DH_input_g);
my $numCores_g = 8;
###################################################################################################################################################################################################
###################################################################################################################################################################################################
say "\n\n\n\n\n\n##################################################################################################";
say "Checking all the necessary softwares in this step......" ;
sub printVersion {
my $software = $_[0];
system("echo '##############################################################################' >> $output2_g/VersionsOfSoftwares.txt 2>&1");
system("echo '#########$software' >> $output2_g/VersionsOfSoftwares.txt 2>&1");
system("$software >> $output2_g/VersionsOfSoftwares.txt 2>&1");
system("echo '\n\n\n\n\n\n' >> $output2_g/VersionsOfSoftwares.txt 2>&1");
}
sub fullPathApp {
my $software = $_[0];
say($software);
system("which $software > yp_my_temp_1.$software.txt");
open(tempFH, "<", "yp_my_temp_1.$software.txt") or die;
my @fullPath1 = <tempFH>;
($#fullPath1 == 0) or die;
system("rm yp_my_temp_1.$software.txt");
$fullPath1[0] =~ s/\n$// or die;
return($fullPath1[0]);
}
my $Picard_g = &fullPathApp("picard.jar");
my $QoRTs_g = &fullPathApp("QoRTs.jar");
&printVersion("kallisto");
&printVersion("salmon");
&printVersion("STAR --version");
&printVersion("hisat2 --version");
&printVersion("subjunc -v");
&printVersion("gsnap --version");
&printVersion("bowtie2 --version");
&printVersion("bwa");
&printVersion("samtools");
&printVersion("fastqc -v");
&printVersion("samstat -v");
&printVersion("preseq");
&printVersion("qualimap -v");
&printVersion("multiqc --version");
&printVersion("propmapped"); ## in subread
&printVersion("qualityScores"); ## in subread
&printVersion("plotFingerprint --version");
&printVersion("goleft -v");
&printVersion("bamtools -v");
&printVersion("rnaseqc --version");
&printVersion("bam_stat.py --version"); ## in RSeQC
&printVersion("geneBody_coverage.py --version"); ## in RSeQC
&printVersion("inner_distance.py --version"); ## in RSeQC
&printVersion("junction_annotation.py --version"); ## in RSeQC
&printVersion("junction_saturation.py --version"); ## in RSeQC
&printVersion("read_distribution.py --version"); ## in RSeQC
&printVersion("read_duplication.py --version"); ## in RSeQC
&printVersion("RPKM_saturation.py --version"); ## in RSeQC
&printVersion("tin.py --version"); ## in RSeQC
&printVersion("java -jar $QoRTs_g");
&printVersion("java -jar $Picard_g CollectIndependentReplicateMetrics --version");
&printVersion("java -jar $Picard_g CollectAlignmentSummaryMetrics --version");
&printVersion("java -jar $Picard_g CollectBaseDistributionByCycle --version");
&printVersion("java -jar $Picard_g CollectGcBiasMetrics --version");
&printVersion("java -jar $Picard_g CollectInsertSizeMetrics --version");
&printVersion("java -jar $Picard_g CollectJumpingLibraryMetrics --version");
&printVersion("java -jar $Picard_g CollectMultipleMetrics --version");
&printVersion("java -jar $Picard_g CollectOxoGMetrics --version");
&printVersion("java -jar $Picard_g CollectQualityYieldMetrics --version");
&printVersion("java -jar $Picard_g CollectSequencingArtifactMetrics --version");
&printVersion("java -jar $Picard_g CollectTargetedPcrMetrics --version");
&printVersion("java -jar $Picard_g CollectWgsMetrics --version");
&printVersion("java -jar $Picard_g EstimateLibraryComplexity --version");
&printVersion("java -jar $Picard_g MeanQualityByCycle --version");
&printVersion("java -jar $Picard_g QualityScoreDistribution --version");
###################################################################################################################################################################################################
###################################################################################################################################################################################################
sub myQC_BAM_1 {
my $dir1 = $_[0]; ## All the SAM files must be in this folder.
my $QCresults = "$dir1/QC_Results";
my $SAMtools = "$QCresults/1_SAMtools";
my $FastQC = "$QCresults/2_FastQC";
my $qualimap = "$QCresults/3_qualimap";
my $samstat = "$QCresults/4_samstat";
my $Bamtools = "$QCresults/5_Bamtools";
my $MultiQC1 = "$QCresults/6_MultiQC1_FastQC";
my $MultiQC2 = "$QCresults/6_MultiQC2_qualimap";
my $MultiQC3 = "$QCresults/6_MultiQC3_SAMtools";
my $MultiQC4 = "$QCresults/6_MultiQC4_Bamtools";
my $MultiQC5 = "$QCresults/6_MultiQC5_Aligner";
&myMakeDir($QCresults);
&myMakeDir($SAMtools);
&myMakeDir($FastQC);
&myMakeDir($qualimap);
&myMakeDir($samstat);
&myMakeDir($Bamtools);
&myMakeDir($MultiQC1);
&myMakeDir($MultiQC2);
&myMakeDir($MultiQC3);
&myMakeDir($MultiQC4);
&myMakeDir($MultiQC5);
opendir(my $FH_Files, $dir1) || die;
my @Files = readdir($FH_Files);
say "\n\n\n\n\n\n##################################################################################################";
say "Detecting the quality of all BAM files by using SAMtools, FastQC, qualimap, samstat, Bamtools and MultiQC ......";
for ( my $i=0; $i<=$#Files; $i++ ) {
next unless $Files[$i] =~ m/\.sam$/;
next unless $Files[$i] !~ m/^[.]/;
next unless $Files[$i] !~ m/[~]$/;
next unless $Files[$i] !~ m/\.toTranscriptome\.out\.bam$/;
next unless $Files[$i] !~ m/^remove/;
my $temp = $Files[$i];
say "\t......$temp";
$temp =~ s/\.sam$// || die;
system("samtools sort -m 2G -o $dir1/$temp.bam --output-fmt bam -T $dir1/yp_$temp --threads $numCores_g $dir1/$temp.sam >>$SAMtools/$temp.runLog 2>&1");
system("samtools index $dir1/$temp.bam >>$SAMtools/$temp.index.runLog 2>&1");
system("samtools flagstat $dir1/$temp.bam >>$SAMtools/$temp.flagstat 2>&1");
system("samtools idxstats $dir1/$temp.bam >>$SAMtools/$temp.idxstats 2>&1");
system( "fastqc --outdir $FastQC --threads $numCores_g --format bam --kmers 7 $dir1/$temp.bam >> $FastQC/$temp.runLog 2>&1" );
system( "qualimap bamqc -bam $dir1/$temp.bam -c -ip -nt $numCores_g -outdir $qualimap/$temp --java-mem-size=16G >> $qualimap/$temp.runLog 2>&1" );
system( "samstat $dir1/$temp.bam >> $samstat/$temp.runLog 2>&1");
system( "rm $dir1/$temp.sam" );
system( "bamtools count -in $dir1/$temp.bam > $Bamtools/bamtools_count.$temp.txt ");
system( "bamtools stats -in $dir1/$temp.bam > $Bamtools/bamtools_stats.$temp.txt ");
}
system( "multiqc --title FastQC --verbose --export --outdir $MultiQC1 $FastQC >> $MultiQC1/MultiQC.FastQC.runLog 2>&1" );
system( "multiqc --title qualimap --verbose --export --outdir $MultiQC2 $qualimap >> $MultiQC2/MultiQC.qualimap.runLog 2>&1" );
system( "multiqc --title SAMtools --verbose --export --outdir $MultiQC3 $SAMtools >> $MultiQC3/MultiQC.SAMtools.runLog 2>&1" );
system( "multiqc --title BAMtools --verbose --export --outdir $MultiQC4 $Bamtools >> $MultiQC4/MultiQC.BAMtools.runLog 2>&1" );
system( "multiqc --title Aligner --verbose --export --outdir $MultiQC5 --ignore QC_Results $dir1 >> $MultiQC5/MultiQC.Aligner.runLog 2>&1" );
}
###################################################################################################################################################################################################
###################################################################################################################################################################################################
sub myQC_BAM_2 {
my $dir1 = $_[0]; ## All the BAM files must be in this folder.
my $QCresults = "$dir1/QC_Results";
my $Fingerprint = "$QCresults/7_Fingerprint";
my $Fingerprint2 = "$QCresults/8_Fingerprint2";
my $goleft = "$QCresults/9_goleft";
my $MultiQC1 = "$QCresults/10_MultiQC_goleft";
&myMakeDir($QCresults);
&myMakeDir($Fingerprint);
&myMakeDir($Fingerprint2);
&myMakeDir($goleft);
&myMakeDir($MultiQC1);
opendir(my $FH_Files, $dir1) || die;
my @Files = readdir($FH_Files);
say "\n\n\n\n\n\n##################################################################################################";
say "Detecting the quality of all BAM files by using plotFingerprint in deepTools, goleft and MultiQC ......";
for ( my $i=0; $i<=$#Files; $i++ ) {
next unless $Files[$i] =~ m/\.bam$/;
next unless $Files[$i] !~ m/^[.]/;
next unless $Files[$i] !~ m/[~]$/;
next unless $Files[$i] !~ m/\.toTranscriptome\.out\.bam$/;
next unless $Files[$i] !~ m/^remove/;
my $temp = $Files[$i];
say "\t......$temp";
$temp =~ s/\.bam$// || die;
system("plotFingerprint --bamfiles $dir1/$temp.bam --numberOfSamples 1000000 --plotFile $Fingerprint/$temp.pdf --plotTitle $temp --outRawCounts $Fingerprint/$temp.cov --outQualityMetrics $Fingerprint/$temp.Metrics.txt --numberOfProcessors $numCores_g --binSize 500 >> $Fingerprint/$temp.runLog 2>&1");
system("plotFingerprint --bamfiles $dir1/$temp.bam --numberOfSamples 1000000 --plotFile $Fingerprint2/$temp.pdf --plotTitle $temp --outRawCounts $Fingerprint2/$temp.cov --outQualityMetrics $Fingerprint2/$temp.Metrics.txt --numberOfProcessors $numCores_g --binSize 5000 >> $Fingerprint2/$temp.runLog 2>&1");
system("goleft covstats $dir1/$temp.bam > $goleft/$temp.covstats " );
system("goleft indexcov --sex chrX,chrY -d $goleft/$temp $dir1/$temp.bam > $goleft/$temp.indexcov.runLog 2>&1" );
}
system("sleep 5s");
system( "multiqc --title goleft --verbose --export --outdir $MultiQC1 $goleft >> $MultiQC1/MultiQC.goleft.runLog 2>&1" );
}
###################################################################################################################################################################################################
###################################################################################################################################################################################################
sub myQC_BAM_3 {
my $dir1 = $_[0]; ## All the BAM files must be in this folder.
my $QCresults = "$dir1/QC_Results";
my $PRESEQ = "$QCresults/11_PRESEQ";
my $PicardDir = "$QCresults/12_Picard";
my $MultiQC1 = "$QCresults/13_MultiQC_PRESEQ";
my $MultiQC2 = "$QCresults/13_MultiQC_Picard";
&myMakeDir($QCresults);
&myMakeDir($PRESEQ);
&myMakeDir($PicardDir);
&myMakeDir($MultiQC1);
&myMakeDir($MultiQC2);
opendir(my $FH_Files, $dir1) || die;
my @Files = readdir($FH_Files);
say "\n\n\n\n\n\n##################################################################################################";
say "Detecting the quality of all BAM files by using PRESEQ, Picard and MultiQC ......";
for ( my $i=0; $i<=$#Files; $i++ ) {
next unless $Files[$i] =~ m/\.bam$/;
next unless $Files[$i] !~ m/^[.]/;
next unless $Files[$i] !~ m/[~]$/;
next unless $Files[$i] !~ m/\.toTranscriptome\.out\.bam$/;
next unless $Files[$i] !~ m/^remove/;
my $temp = $Files[$i];
say "\t......$temp";
$temp =~ s/\.bam$// || die;
system("preseq c_curve -output $PRESEQ/$temp.c_curve.pe.PRESEQ -step 1000000 -verbose -pe -bam $dir1/$temp.bam >> $PRESEQ/$temp.c_curve.pe.runLog 2>&1");
system("preseq c_curve -output $PRESEQ/$temp.c_curve.se.PRESEQ -step 1000000 -verbose -bam $dir1/$temp.bam >> $PRESEQ/$temp.c_curve.se.runLog 2>&1");
system("preseq lc_extrap -output $PRESEQ/$temp.lc_extrap.pe.PRESEQ -step 1000000 -verbose -pe -bam $dir1/$temp.bam >> $PRESEQ/$temp.lc_extrap.pe.runLog 2>&1");
system("preseq lc_extrap -output $PRESEQ/$temp.lc_extrap.se.PRESEQ -step 1000000 -verbose -bam $dir1/$temp.bam >> $PRESEQ/$temp.lc_extrap.se.runLog 2>&1");
&myMakeDir("$PicardDir/$temp");
#system("java -jar $Picard_g CollectIndependentReplicateMetrics INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/0_CollectIndependentReplicateMetrics VCF=null MINIMUM_MQ=20 >> $PicardDir/$temp/0.runLog 2>&1" );
system("java -jar $Picard_g CollectAlignmentSummaryMetrics INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/1_CollectAlignmentSummaryMetrics >> $PicardDir/$temp/1.runLog 2>&1" );
system("java -jar $Picard_g EstimateLibraryComplexity INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/2_EstimateLibraryComplexity >> $PicardDir/$temp/2.runLog 2>&1" );
system("java -jar $Picard_g CollectInsertSizeMetrics INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/3_CollectInsertSizeMetrics HISTOGRAM_FILE=$PicardDir/$temp/3.pdf MINIMUM_PCT=0.05 >> $PicardDir/$temp/3.runLog 2>&1" );
system("java -jar $Picard_g CollectJumpingLibraryMetrics INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/4_CollectJumpingLibraryMetrics >> $PicardDir/$temp/4.runLog 2>&1" );
system("java -jar $Picard_g CollectMultipleMetrics INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/5_CollectMultipleMetrics >> $PicardDir/$temp/5.runLog 2>&1" );
system("java -jar $Picard_g CollectBaseDistributionByCycle INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/6_CollectBaseDistributionByCycle CHART_OUTPUT=$PicardDir/$temp/6.pdf >> $PicardDir/$temp/6.runLog 2>&1" );
system("java -jar $Picard_g CollectQualityYieldMetrics INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/7_CollectQualityYieldMetrics >> $PicardDir/$temp/7.runLog 2>&1" );
#system("java -jar $Picard_g CollectWgsMetrics INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/8_CollectWgsMetricsFromQuerySorted REFERENCE_SEQUENCE=null >> $PicardDir/$temp/8.runLog 2>&1" );
system("java -jar $Picard_g MeanQualityByCycle INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/9_MeanQualityByCycle CHART_OUTPUT=$PicardDir/$temp/9.pdf >> $PicardDir/$temp/9.runLog 2>&1" );
system("java -jar $Picard_g QualityScoreDistribution INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/10_QualityScoreDistribution CHART_OUTPUT=$PicardDir/$temp/10.pdf >> $PicardDir/$temp/10.runLog 2>&1" );
#system("java -jar $Picard_g CollectGcBiasMetrics INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/11_CollectGcBiasMetrics CHART_OUTPUT=$PicardDir/$temp/11.pdf SUMMARY_OUTPUT=$PicardDir/$temp/11.summary.output >> $PicardDir/$temp/11.runLog 2>&1" );
#system("java -jar $Picard_g CollectOxoGMetrics INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/12_CollectOxoGMetrics REFERENCE_SEQUENCE=null >> $PicardDir/$temp/12.runLog 2>&1" );
#system("java -jar $Picard_g CollectSequencingArtifactMetrics INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/13_CollectSequencingArtifactMetrics >> $PicardDir/$temp/13.runLog 2>&1" );
#system("java -jar $Picard_g CollectTargetedPcrMetrics INPUT=$dir1/$temp.bam OUTPUT=$PicardDir/$temp/14_CollectTargetedPcrMetrics >> $PicardDir/$temp/14.runLog 2>&1" );
}
system( "multiqc --title PRESEQ --verbose --export --outdir $MultiQC1 $PRESEQ >> $MultiQC1/MultiQC.PRESEQ.runLog 2>&1" );
system( "multiqc --title Picard --verbose --export --outdir $MultiQC2 $PicardDir >> $MultiQC2/MultiQC.Picard.runLog 2>&1" );
}
###################################################################################################################################################################################################
###################################################################################################################################################################################################
sub myQC_BAM_4 {
my $dir1 = $_[0]; ## All the BAM files must be in this folder.
my $QCresults = "$dir1/QC_Results";
my $SubreadUti= "$QCresults/14_SubreadUti";
&myMakeDir("$QCresults");
&myMakeDir("$SubreadUti");
opendir(my $DH_map, $dir1) || die;
my @mapFiles = readdir($DH_map);
say "\n\n\n\n\n\n##################################################################################################";
say "Detecting the quality of bam files by using Subreads utilities ......";
for (my $i=0; $i<=$#mapFiles; $i++) {
next unless $mapFiles[$i] =~ m/\.bam$/;
next unless $mapFiles[$i] !~ m/^[.]/;
next unless $mapFiles[$i] !~ m/[~]$/;
next unless $mapFiles[$i] !~ m/\.toTranscriptome\.out\.bam$/;
next unless $mapFiles[$i] !~ m/^remove/;
my $temp = $mapFiles[$i];
$temp =~ s/\.bam$// || die;
say "\t......$mapFiles[$i]";
system("propmapped -i $dir1/$temp.bam -o $SubreadUti/$temp.prommapped >> $SubreadUti/$temp.prommapped 2>&1");
system("echo '\n\n\n\n\n' >> $SubreadUti/$temp.prommapped 2>&1");
system("propmapped -i $dir1/$temp.bam -f -o $SubreadUti/$temp.prommapped >> $SubreadUti/$temp.prommapped 2>&1");
system("echo '\n\n\n\n\n' >> $SubreadUti/$temp.prommapped 2>&1");
system("propmapped -i $dir1/$temp.bam -f -p -o $SubreadUti/$temp.prommapped >> $SubreadUti/$temp.prommapped 2>&1");
system("qualityScores --BAMinput -i $dir1/$temp.bam -o $SubreadUti/$temp.qualityScores >> $SubreadUti/$temp.qualityScores 2>&1");
}
}
###################################################################################################################################################################################################
###################################################################################################################################################################################################
sub myQC_BAM_RNA {
my $dir1 = $_[0]; ## All the BAM files must be in this folder.
my $QCresults = "$dir1/QC_Results";
my $QoRTs = "$QCresults/RNA_1_QoRTs";
my $RSeQC = "$QCresults/RNA_2_RSeQC";
my $RNA_SeQC = "$QCresults/RNA_3_RNA-SeQC";
my $MultiQC1 = "$QCresults/RNA_4_MultiQC1_RSeQC";
my $MultiQC2 = "$QCresults/RNA_4_MultiQC2_RNA-SeQC";
my $MultiQC3 = "$QCresults/RNA_4_MultiQC3_QoRTs";
&myMakeDir($QCresults);
&myMakeDir($QoRTs);
&myMakeDir($RSeQC);
&myMakeDir($RNA_SeQC);
&myMakeDir($MultiQC1);
&myMakeDir($MultiQC2);
&myMakeDir($MultiQC3);
opendir(my $FH_Files, $dir1) || die;
my @Files = readdir($FH_Files);
say "\n\n\n\n\n\n##################################################################################################";
say "Detecting the quality of all BAM files by using QoRTs, RSeQC, RNA-SeQC and MultiQC ......";
for ( my $i=0; $i<=$#Files; $i++ ) {
next unless $Files[$i] =~ m/\.bam$/;
next unless $Files[$i] !~ m/^[.]/;
next unless $Files[$i] !~ m/[~]$/;
next unless $Files[$i] !~ m/\.toTranscriptome\.out\.bam$/;
next unless $Files[$i] !~ m/^remove/;
my $temp = $Files[$i];
say "\t......$temp";
$temp =~ s/\.bam$// || die;
my $GTF = "/home/yp/.MyProgramFiles/2_Aligners/STAR/RefGenome_Gencode/$genome_g.gencode.gtf";
&myMakeDir("$QoRTs/$temp");
system("java -jar $QoRTs_g QC --generatePlots $dir1/$temp.bam $GTF $QoRTs/$temp >> $QoRTs/$temp.runLog 2>&1");
&myMakeDir("$RSeQC/$temp");
system("tin.py --input=$dir1/$temp.bam --refgene=0-Other/GenesBED/$genome_g/$genome_g.UCSC_knownGene.bed >> $RSeQC/$temp/1-tin.runLog 2>&1");
system("bam_stat.py --input-file=$dir1/$temp.bam >> $RSeQC/$temp/2-bam_stat.runLog 2>&1");
system("clipping_profile.py --input-file=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/3-clipping_profile --sequencing=PE >> $RSeQC/$temp/3-clipping_profile.runLog 2>&1");
system("deletion_profile.py --input=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/4-deletion_profile --read-align-length=150 >> $RSeQC/$temp/4-deletion_profile.runLog 2>&1");
system("geneBody_coverage.py --input=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/5-geneBody_coverage --refgene=0-Other/GenesBED/$genome_g/$genome_g.UCSC_knownGene.bed >> $RSeQC/$temp/5-geneBody_coverage.runLog 2>&1");
system("inner_distance.py --input-file=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/6-inner_distance --refgene=0-Other/GenesBED/$genome_g/$genome_g.UCSC_knownGene.bed >> $RSeQC/$temp/6-inner_distance.runLog 2>&1");
system("insertion_profile.py --input-file=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/7-insertion_profile --sequencing=PE >> $RSeQC/$temp/7-insertion_profile.runLog 2>&1");
system("junction_annotation.py --input-file=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/8-junction_annotation --refgene=0-Other/GenesBED/$genome_g/$genome_g.UCSC_knownGene.bed >> $RSeQC/$temp/8-junction_annotation.runLog 2>&1");
system("junction_saturation.py --input-file=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/9-junction_saturation --refgene=0-Other/GenesBED/$genome_g/$genome_g.UCSC_knownGene.bed >> $RSeQC/$temp/9-junction_saturation.runLog 2>&1");
system("mismatch_profile.py --input=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/10-mismatch_profile --read-align-length=150 >> $RSeQC/$temp/10-mismatch_profile.runLog 2>&1");
system("read_distribution.py --input-file=$dir1/$temp.bam --refgene=0-Other/GenesBED/$genome_g/$genome_g.UCSC_knownGene.bed >> $RSeQC/$temp/11-read_distribution.runLog 2>&1");
system("read_duplication.py --input-file=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/11-read_duplication >> $RSeQC/$temp/12-read_duplication.runLog 2>&1");
system("read_GC.py --input-file=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/12-read_GC >> $RSeQC/$temp/13-read_GC.runLog 2>&1");
system("read_NVC.py --input-file=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/13-read_NVC --nx >> $RSeQC/$temp/14-read_NVC.runLog 2>&1");
system("read_quality.py --input-file=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/14-read_quality >> $RSeQC/$temp/15-read_quality.runLog 2>&1");
system("RPKM_saturation.py --input-file=$dir1/$temp.bam --out-prefix=$RSeQC/$temp/15-RPKM_saturation --refgene=0-Other/GenesBED/$genome_g/$genome_g.UCSC_knownGene.bed >> $RSeQC/$temp/16-RPKM_saturation.runLog 2>&1");
system("rnaseqc $GTF $dir1/$temp.bam $RNA_SeQC/$temp >> $RNA_SeQC/$temp.runLog 2>&1");
}
system( "multiqc --title RSeQC --verbose --export --outdir $MultiQC1 $RSeQC >> $MultiQC1/MultiQC.RSeQC.runLog 2>&1" );
system( "multiqc --title RNA_SeQC --verbose --export --outdir $MultiQC2 $RNA_SeQC >> $MultiQC2/MultiQC.RNA_SeQC.runLog 2>&1" );
system( "multiqc --title QoRTs --verbose --export --outdir $MultiQC3 $QoRTs >> $MultiQC3/MultiQC.QoRTs.runLog 2>&1" );
}
###################################################################################################################################################################################################
###################################################################################################################################################################################################
say "\n\n\n\n\n\n##################################################################################################";
say "Detecting BAM files in input folder ......";
my @BAMfiles_g = ();
{
open(seqFiles_FH, ">", "$output2_g/BAM-Files.txt") or die;
for ( my $i=0; $i<=$#inputFiles_g; $i++ ) {
next unless $inputFiles_g[$i] =~ m/\.bam$/;
next unless $inputFiles_g[$i] !~ m/^[.]/;
next unless $inputFiles_g[$i] !~ m/[~]$/;
next unless $inputFiles_g[$i] !~ m/unpaired/;
next unless $inputFiles_g[$i] !~ m/^remove/;
say "\t......$inputFiles_g[$i]";
$BAMfiles_g[$#BAMfiles_g+1] = $inputFiles_g[$i];
say "\t\t\t\tBAM file: $inputFiles_g[$i]\n";
say seqFiles_FH "BAM file: $inputFiles_g[$i]\n";
}
say seqFiles_FH "\n\n\n\n\n";
say seqFiles_FH "All BAM files:@BAMfiles_g\n\n\n";
say "\t\t\t\tAll BAM files:@BAMfiles_g\n\n";
my $num1 = $#BAMfiles_g + 1;
say seqFiles_FH "\nThere are $num1 BAM files.\n";
say "\t\t\t\tThere are $num1 BAM files.\n";
}
###################################################################################################################################################################################################
###################################################################################################################################################################################################
sub myFilterSAM {
my $folder = $_[0]; ## input and output dir
my $filePre = $_[1]; ## prefix of name of input and out file
open(FILE1, "<", "$folder/$filePre.t.sam") or die "$!";
open(FILE2, ">", "$folder/$filePre.sam") or die "$!";
open(FILE3, ">", "$folder/removed_$filePre.sam") or die "$!";
open(FILE4, ">", "$folder/num_mismatches_$filePre.txt") or die "$!";
my $n1 = 0; ## all reads
my $n2 = 0; ## kept reads
my $n3 = 0; ## removed reads
while (my $line1=<FILE1>) {
if ($line1 =~ m/^@/) {
print FILE2 $line1 ;
print FILE3 $line1 ;
}else{
$line1 =~ m/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+/ or die;
my $chr = $3;
my $length1 = length($10);
$n1++;
if( ($chr =~ m/^chr[XYM\d]+/i) and ($length1 > 10) ) {
print FILE2 $line1; $n2++;
}else{
print FILE3 $line1; $n3++;
}
my $num = "NA";
$line1 =~ m/\s+NM\:i\:(\d+)\s+/ ;
$num = $1;
print FILE4 "$num\n";
}
}
print "\t\tall reads in $filePre: $n1\n";
print "\t\tkept reads in $filePre: $n2\n";
print "\t\tremoved reads in $filePre: $n3\n\n\n";
close FILE1;
close FILE2;
close FILE3;
close FILE4;
}
###################################################################################################################################################################################################
###################################################################################################################################################################################################
say "\n\n\n\n\n\n##################################################################################################";
say "Removing some reads ......";
for (my $i=0; $i<=$#BAMfiles_g; $i++) {
my $temp = $BAMfiles_g[$i];
$temp =~ s/\.bam$// || die;
say "\t......$BAMfiles_g[$i]";
system("samtools view -h --threads $numCores_g -q 0 -o $output_g/$temp.t.sam $input_g/$temp.bam >> $output2_g/$temp.runLog 2>&1 ");
&myFilterSAM($output_g, $temp);
system("rm $output_g/$temp.t.sam");
}
###################################################################################################################################################################################################
###################################################################################################################################################################################################
&myQC_BAM_1($output_g);
&myQC_BAM_2($output_g);
&myQC_BAM_RNA($output_g);
&myQC_BAM_3($output_g);
&myQC_BAM_4($output_g);
###################################################################################################################################################################################################
###################################################################################################################################################################################################
say "\n\n\n\n\n\n##################################################################################################";
say "\tJob Done! Cheers! \n\n\n\n\n";
## END