-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCWAP_2017a.pl
executable file
·1738 lines (1587 loc) · 97.7 KB
/
CWAP_2017a.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
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
#A WES pipline for BGISEQ-500
#author: Song Bin, [email protected]
use strict;
use warnings;
use FindBin '$Bin';
use FileHandle;
use File::Basename qw/dirname basename/;
die "Usage:\tperl $0 <Fastq list> <Config file> <Outdir> \n"if(@ARGV!=3);
my($fqlist,$config,$outdir)=@ARGV;
`mkdir -p $outdir`;
################################### main parameters ###################################
my(%configs,%Splitbeds,%SplitbedCount);
my(%rawfqs,%cleanfqs,%cleanList);
my(%bwaBams,%rmdupBams,%realnBams,%brecalBams,%sampleBams,%mergeBams);
my(%patientIds,%samplePairs,%patients2samples,%edge_sampair);
my(%maxmems); ## max memory use for co-realnment
my @chrs=();
foreach my $i("M",1..22,"X","Y"){push @chrs,"chr$i";}
################################ Process sequence #######################################
&readConfig($config);
&readFqlist($fqlist);
if ($configs{FastqClean} =~ /true/i){
&FastqClean(\%rawfqs,\%cleanfqs);
}
if ($configs{Aligment} ne "false"){
if($configs{Aligment} eq "edico"){
&Aligment_Edico(\%cleanList);
}elsif($configs{Aligment} =~ /bwa/){
&Aligment_BWA(\%cleanfqs);
}
}
if ($configs{MarkDuplicates} eq "true"){
&MarkDuplicates(\%bwaBams);
}
if(defined $configs{Corealn} && $configs{RealnType} eq "co-realn"){
&PatientRealn($configs{Corealn});
&BaseRecalibrator(\%realnBams);
}elsif($configs{RealnType} eq "realn"){
&GATKrealnRecal(\%rmdupBams);
}
if ($configs{mergeBrecalBAM} =~ /true/i){
&mergeBrecalBAM(\%brecalBams,\%mergeBams);
}
if ($configs{bamdstQC} =~ /true/i){
&bamdstQC(\%sampleBams);
}
if ($configs{TargetQC} =~ /true/i){
&TargetQC(\%sampleBams);
}
#if ($configs{DNA_damage} =~ /true/i){
# &DNA_damage(\%sampleBams);
#}
######################### Germline variant calling: snps,indels ##########################
if($configs{gatkDISCOVERY}=~/true/i){
foreach my $samp (sort keys %brecalBams){
&makedir("$outdir/$samp/gatkDISCOVERY");
foreach my $chr (sort keys %{$brecalBams{$samp}}){
&gatkDISCOVERY($samp,$brecalBams{$samp}{$chr},$chr);
}
&mergeGATKVars($samp);
}
}
if($configs{gatkGVCF}=~/true/i){
foreach my $samp (sort keys %brecalBams){
&makedir("$outdir/$samp/gatkGVCF");
if($configs{Aligment} eq "edico"){
&gatkGVCF_GenotypeGVCFs($samp);
}else{
foreach my $chr (sort keys %{$brecalBams{$samp}}){
&gatkGVCF_sample($samp,$brecalBams{$samp}{$chr},$chr);
}
&gatkGVCF_merge($samp);
}
&gatkGVCF_process($samp);
}
&makedir("$outdir/combine/gatkGVCF") if(!-e "$outdir/combine/gatkGVCF");
if($configs{Aligment} eq "edico"){
&gatkGVCF_combine_bysample("$outdir/combine/gatkGVCF");
}else{
&gatkGVCF_combine_bychr("$outdir/combine/gatkGVCF");
&gatkGVCF_merge("combine");
}
&gatkGVCF_VQSR("combine");
&gatkGVCF_process("combine");
if($configs{patients}){
open IN,"$configs{patients}" or die $!;
while(<IN>){
chomp;
my @F = split /\t/;
my $p = shift @F;
@{$patients2samples{$p}} = @F;
}
close IN;
gatkGVCF_combineGTs(\%patients2samples,\%configs,$outdir);
}
}
######################## Somatic variant calling: Ssnv, Sindel #########################
if($configs{somatic}){
open SMTF,"$configs{somatic}" or die $!;
while(<SMTF>){
chomp;
my ($patient,$normal,$tumor)=(split /\s+/)[0,1,2];
die "bam files of $normal don't exists!\n" if(!exists $brecalBams{$normal});
die "bam files of $tumor don't exists!\n" if(!exists $brecalBams{$tumor});
my $samplePair="$normal-VS-$tumor";
`mkdir -p $outdir/somatic/$samplePair` if(!-e "$outdir/somatic/$samplePair");
$samplePairs{$samplePair}{normal}=$normal;
$samplePairs{$samplePair}{tumor} =$tumor;
}
close SMTF;
if($configs{mutectSNV}=~/true/i){
&somatic_batch_byChr(\%brecalBams,\%samplePairs,\&mutectSNV,\&mutectSNV_process,"mutectSNV");
}
if($configs{mutect2}=~/true/i){
if($configs{seqType} =~ /WES/i){
&somatic_batch_byRegion(\%brecalBams,\%samplePairs,\&RunMutect2,\&mutect2_process,"mutect2");
}elsif($configs{seqType} =~ /WGS/i){
&somatic_batch_byChr(\%brecalBams,\%samplePairs,\&RunMutect2,\&mutect2_process,"mutect2");
}
}
if($configs{varscanSNV}=~/true/i){
&somatic_batch_byChr(\%brecalBams,\%samplePairs,\&varscanSNV,\&varscanSNV_process,"varscanSNV");
}
if($configs{gatkSindel}=~/true/i){
&somatic_batch_byChr(\%brecalBams,\%samplePairs,\&gatkSindel,\&gatkSindel_process,"gatkSindel");
}
if($configs{platypusSindel}=~/true/i){
&somatic_batch_byChr(\%brecalBams,\%samplePairs,\&platypusSindel,\&platypusSindel_process,"platypusSindel");
}
if($configs{ADTExSCNV}=~/true/i){
&somatic_batch_bySamplePair(\%brecalBams,\%samplePairs,\&ADTExSCNV);
}
if($configs{ABSOLUTE}=~/true/i){
&somatic_batch_bySamplePair(\%brecalBams,\%samplePairs,\&ABSOLUTE);
}
}
############################### monitor to control the pipeline #######################
if($configs{edgeList}=~/true/i){
&edgeList(); ##creat scrips to monitor
&edgeList2();
&edgeList_Mutect2();
}
if($configs{shlist}=~/true/i){
&shlist();
}
if($configs{rmlist}=~/true/i){
&rmlist();
}
################################ sub function #########################
## This Function is using to generate script.
sub generateShell{
my ($output_shell, $content, $finish_string) = @_;
unlink glob "$output_shell.*" if($configs{rmlog} eq "true");
$finish_string ||= "Still_waters_run_deep";
open OUT,">$output_shell" or die "Cannot open file $output_shell:$!";
print OUT "#!/bin/bash\n";
print OUT "echo ==========start at : `date` ==========\n";
print OUT "$content && \\\n";
print OUT "echo ==========end at : `date` ========== && \\\n";
print OUT "echo $finish_string 1>&2 && \\\n";
print OUT "echo $finish_string > $output_shell.sign\n";
close OUT;
}
sub makedir{
my ($dir)=@_;
if(!-e $dir){
`mkdir -p $dir`;
`mkdir $dir/shell`;
`mkdir $dir/tmp`;
`mkdir $dir/raw`;
`mkdir $dir/result`;
}
}
sub readConfig{
my ($config) = @_;
open CFG,"$config" or die $!;
while(<CFG>){
chomp;
if($_=~/^\#/){next;}
if($_=~/^(.*)=(.+);.?/){
$configs{$1}=$2;
}
}
close CFG;
$configs{home} ||=$Bin;
$configs{bin} ||="$configs{home}/bin";
$configs{database} ||="$configs{home}/database";
$configs{lib} ||="$configs{home}/lib";
$configs{reference} ||="$configs{database}/reference/hg19/hg19.fa";
$configs{targetRegion} ||="$configs{database}/targetRegion/BGI_exome_V4_region.bed";
$configs{Splitbed} ||="$configs{database}/targetRegion/BGIv4_TR/Splitbed/";
## GATK_bundle
$configs{bundle} ||="$configs{database}/GATK_bundle";
$configs{GATKdbsnp} ||="$configs{bundle}/dbsnp_138.hg19.vcf.gz";
$configs{GATKdbsnp150} ||="$configs{bundle}/dbsnp_150.hg19.vcf.gz";
$configs{GATKomni} ||="$configs{bundle}/1000G_omni2.5.hg19.vcf.gz";
$configs{GATKkgSNP} ||="$configs{bundle}/1000G_phase1.snps.high_confidence.hg19.vcf.gz";
$configs{GATKkgIndel} ||="$configs{bundle}/1000G_phase1.indels.hg19.vcf.gz";
$configs{GATKmillsIndel} ||="$configs{bundle}/Mills_and_1000G_gold_standard.indels.hg19.vcf.gz";
$configs{GATKhapmap} ||="$configs{bundle}/hapmap_3.3.hg19.vcf.gz";
## mutect dbsnp and cosmic
$configs{mutectDbsnp} ||="$configs{bundle}/dbsnp_138.hg19.vcf.gz";
#$configs{mutectCosmic} ||="$configs{bundle}/cosmic_v79.hg19.vcf.gz";
$configs{mutectCosmic} ||="$configs{bundle}/cosmic_v82.hg19.vcf.gz";
##annovar
$configs{annovar} ||="$configs{bin}/ANNOVAR/v20170716";
$configs{annovardb} ||="$configs{bin}/ANNOVAR/humandb20170901";
$configs{annovarPara} ||="-remove -protocol refGene,ensGene,knownGene,cytoBand,genomicSuperDups,gwasCatalog,phastConsElements100way,phastConsElements46way,targetScanS,tfbsConsSites,wgRna,avsnp147,clinvar_20170130,cosmic82_coding,cosmic82_noncoding,dbnsfp33a,exac03nonpsych,exac03nontcga,gnomad_exome,gnomad_genome,popfreq_all_20150413,intervar_20170202 -operation g,g,g,r,r,r,r,r,r,r,r,f,f,f,f,f,f,f,f,f,f,f -nastring . -vcfinput";
## tools
$configs{java8} ||="$configs{bin}/java";
$configs{java} ||="/usr/java/latest/bin/java";
$configs{SOAPnuke} ||="$configs{bin}/SOAPnuke";
$configs{fqcheck} ||="$configs{bin}/fqcheck33";
$configs{bwa} ||="$configs{bin}/bwa";
$configs{samtools} ||="$configs{bin}/samtools";
$configs{bcftools} ||="$configs{bin}/bcftools";
$configs{picard} ||="$configs{bin}/picard.jar";
$configs{GATK} ||="$configs{bin}/GenomeAnalysisTK.jar";
$configs{GATK23} ||="$configs{bin}/GenomeAnalysisTK-2.3.jar";
$configs{mutect} ||="$configs{bin}/mutect-1.1.7.jar";
$configs{Platypus} ||="$configs{bin}/Platypus_0.8.1";
$configs{varscan} ||="$configs{bin}/VarScan.v2.4.3.jar";
$configs{monitor} ||="$configs{bin}/monitor";
$configs{PanCanQC} ||="$configs{bin}/PanCanQC-1.2.2";
## python path
$configs{pythonBin}="$configs{bin}/Anaconda2-4.3.0/bin";
$configs{pythonLib}="$configs{bin}/Anaconda2-4.3.0/lib";
$configs{htslib}="$configs{lib}/htslib";
$configs{pythonPath}="$configs{bin}/Anaconda2-4.3.0/lib/python2.7/site-packages";
## clean ,bwa-mem and picard parameters
$configs{platformPara} ||="COMPLETE";
#$configs{SOAPnukePara} ||= "-n 0.1 -q 0.5 -l 5 -Q 2 -E 35 -G -f AAGTCGGAGGCCAAGCGGTCTTAGGAAGACAA -r CAACTCCTTGGCTCACAGAACGACATGGCTACGATCCGACTT -M 2";
$configs{SOAPnukePara} ||= "-n 0.1 -q 0.5 -l 5 -Q 2 -E 35 -G -f AAGTCGGAGGCCAAGCGGTCTTAGGAAGACAA -r AAGTCGGATCGTAGCCATGTCGTTCTGTGAGCCAAGGAGTTG -M 2"; #modify at 20171017
$configs{bwaMemPara} ||= "-t 6 -k 32";
$configs{bwaAlnPara} ||= "-o 1 -e 50 -m 100000 -l 32 -k 2 -t 4 -L -i 15";
$configs{bwaSamPara} ||= "";
$configs{picardRmdupPara} ||= "REMOVE_DUPLICATES=false VALIDATION_STRINGENCY=SILENT ASSUME_SORTED=true PROGRAM_RECORD_ID=null";
$configs{covdepPara} ||= "--maxdepth 1000 -q 1";
## Varscan somatic parameters
$configs{samtoolsMpileupPara}||="-q 20 -Q 20 -B";
$configs{varscanPara}||="--output-vcf 1 --strand-filter 1 --min-reads2 2 --min-avg-qual 20 --min-var-freq 0.001 --min-freq-for-hom 0.75 --somatic-p-value 0.05 --normal-purity 1.0 --tumor-purity 1.0 --p-value 0.99 --mpileup 1";
## Platypus sindel parameters
$configs{PlatypusPara} ||= "--filterDuplicates=1 --nCPU=1 --logFileName=/dev/null";
$configs{PlatypussmtStrictPara} ||= "--minPosterior 5";
## gatk sindel parameters
$configs{GATKsomaticIndelPara} ||= "--window_size 300";
$configs{GATKsmtFlitPara} ||= "--RStartTh 5 --RendTh 5";
$configs{seqType} ||= "WES";
$configs{readLen} ||= "PE50";
$configs{rmlog} ||= "true";
$configs{FastqClean} ||= "true";
$configs{Aligment} ||= "edico";
$configs{rmCleanReads} ||= "true";
$configs{MarkDuplicates} ||= "true";
$configs{RealnType} ||= "realn";
$configs{mergeBrecalBAM} ||= "false";
$configs{bamdstQC} ||= "true";
$configs{TargetQC} ||= "true";
$configs{DNA_damage} ||= "true";
$configs{gatkDISCOVERY} ||= "false";
$configs{gatkGVCF} ||= "false";
$configs{mutectSNV} ||= "true";
$configs{mutect2} ||= "true";
$configs{varscanSNV} ||= "true";
$configs{gatkSindel} ||= "true";
$configs{platypusSindel} ||= "true";
$configs{ADTExSCNV} ||= "true";
$configs{ABSOLUTE} ||= "true";
$configs{edgeList} ||= "true";
$configs{shlist} ||= "true";
$configs{rmlist} ||= "true";
# file exist check
foreach my $key (sort keys %configs){
next if($key=~/Para/);
if($configs{$key}=~/\// && !-e $configs{$key}){
print "Warning: Value of $key don't exist! $configs{$key}\n";
}
}
# value check
if($configs{targetRegion} =~ /BGI_exome_V4/){
`if [ -e $outdir/TR ];then rm -rf $outdir/TR;fi`;
`ln -s $configs{database}/targetRegion/BGIv4_TR $outdir/TR`;
}
elsif($configs{USER_TR}){
`if [ -e $outdir/TR ];then rm -rf $outdir/TR;fi`;
`ln -s $configs{USER_TR} $outdir/TR`;
}
else{
die "targetRegion only support BGI_exome_V4 now\n";
}
if ($configs{somatic}){
foreach my $chr(@chrs){
my @files = glob "$configs{Splitbed}/$chr.list.*";
@{$Splitbeds{$chr}}=@files;
$SplitbedCount{$chr} = scalar @files;
}
}
}
## parse targetRegion for gatk
sub parseTR{
my $tr=shift;
`mkdir -p $outdir/TR`;
`mkdir $outdir/TR/interval`;
`mkdir $outdir/TR/bed`;
open TR,"$tr" or die $!;
my (%outInt,%outBed);
foreach my $i (@chrs){
open $outInt{$i},">$outdir/TR/interval/$i.intervals" or die $!;
open $outBed{$i},">$outdir/TR/bed/$i.bed" or die $!;
}
while(<TR>){
chomp;
my ($c,$start,$end)=split /\s+/,$_;
$outInt{$c}->print("$c:$start-$end\n");
$outBed{$c}->print("$c\t$start\t$end\n");
}
close TR;
foreach my $chr(@chrs){
undef $outInt{$chr};
undef $outBed{$chr};
}
}
sub readFqlist{
my ($fqlist) = @_;
open LL,$fqlist or die $!;
while(<LL>){
chomp;
my @F=split /\t/;
die "Wrong format of $fqlist!" if(scalar(@F)!=7);
my ($samp,$lib,$laneIndex,$fqfiles,$readLen,$insert,$baseNum)=@F[0,1,2,3,4,5,6];
my $dir="$outdir/$samp/$laneIndex";
my ($fq1,$fq2)=(split /,/,$fqfiles)[0,1];
$rawfqs{$samp}{$lib}{$laneIndex}{1}=$fq1;
$rawfqs{$samp}{$lib}{$laneIndex}{2}=$fq2;
my $cleanfq1="$dir/raw/$laneIndex\_1.clean.fq.gz";
my $cleanfq2="$dir/raw/$laneIndex\_2.clean.fq.gz";
$cleanfqs{$samp}{$lib}{$laneIndex}{1}=$cleanfq1;
$cleanfqs{$samp}{$lib}{$laneIndex}{2}=$cleanfq2;
my ($chip,$lane,$index)=(split /_/,$laneIndex)[0,1,2];
my $rgid="$chip\_$lane";
push @{$cleanList{$samp}}, "$rgid,$samp,$lib,$lane,$configs{platformPara},BGI,$cleanfq1,$cleanfq2";
$bwaBams{$samp}{$lib}{$laneIndex}="$dir/result/$laneIndex.sort.bam";
if(!exists $rmdupBams{$samp}){
$rmdupBams{$samp} = "$outdir/$samp/rmdup/result/$samp.rmdup.bam";
foreach my $chr (@chrs){
$realnBams{$samp}{$chr} = "$outdir/$samp/realn/result/$samp.$chr.realn.bam";
$brecalBams{$samp}{$chr} = "$outdir/$samp/brecal/result/$samp.$chr.brecal.bam"
}
if($configs{Aligment} eq "edico"){
$sampleBams{$samp} = "$outdir/$samp/edico/result/$samp.mkdup.bam";
}else{
$sampleBams{$samp} = "$outdir/$samp/brecal/result/$samp.bam";
}
if($configs{mergeBrecalBAM} eq "true"){
$mergeBams{$samp} = "$outdir/$samp/brecal/result/$samp.bam";
}
}
}
close LL;
}
sub FastqClean{
my ($hashref1,$hashref2) = @_;
my %rawfqs = %$hashref1;
my %cleanfqs = %$hashref2;
foreach my $samp (sort keys %rawfqs){
foreach my $lib (sort keys %{$rawfqs{$samp}}){
foreach my $laneIndex (sort keys %{$rawfqs{$samp}{$lib}}){
my $dir="$outdir/$samp/$laneIndex";
makedir($dir) if(!-e $dir);;
my $fq1=$rawfqs{$samp}{$lib}{$laneIndex}{1};
my $fq2=$rawfqs{$samp}{$lib}{$laneIndex}{2};
my $cleanfq1=$cleanfqs{$samp}{$lib}{$laneIndex}{1};
my $cleanfq2=$cleanfqs{$samp}{$lib}{$laneIndex}{2};
&peLaneClean($samp,$laneIndex,$dir,$fq1,$fq2,$cleanfq1,$cleanfq2);
}
}
}
}
sub Aligment_BWA{
my ($hashref) = @_;
my %cleanfqs = %$hashref;
foreach my $samp (sort keys %cleanfqs){
foreach my $lib (sort keys %{$cleanfqs{$samp}}){
foreach my $laneIndex (sort keys %{$cleanfqs{$samp}{$lib}}){
my $dir="$outdir/$samp/$laneIndex";
my $cleanfq1=$cleanfqs{$samp}{$lib}{$laneIndex}{1};
my $cleanfq2=$cleanfqs{$samp}{$lib}{$laneIndex}{2};
my ($chip,$lane,$index)=(split /_/,$laneIndex)[0,1,2];
my $rgid="$chip\_$lane";
if($configs{Aligment} eq "bwa_aln"){
&BWA_aln($samp,$cleanfq1,$cleanfq2,$laneIndex,$lib,$rgid,$dir);
}
elsif($configs{Aligment} eq "bwa_mem"){
&BWA_mem($samp,$cleanfq1,$cleanfq2,$laneIndex,$lib,$rgid,$dir);
}
if($configs{Aligment} =~ /bwa/){
&laneQC($samp,$laneIndex,$lib,$dir);
}
}
}
}
}
# from /hwfssz1/ST_CANCER/POL/SHARE/CancerPipeline/Edico_BGISEQ_v1.0/CancerPipeline_Edico_BGISEQ_v1.0.pl
sub Aligment_Edico{
my ($hashref) = @_;
my %cleanList = %$hashref;
# default /staging/human/reference/hg19/hg19.fa
$configs{vc_reference} = $configs{reference};
if ($configs{readLen} =~ /PE50/){
$configs{hash_table} = "$configs{database}/reference/hg19.fa.k_19";
}elsif($configs{readLen} =~ /PE100/){
#$hash_table = "/staging/examples/reference/hg19/hg19.fa.k_21.f_16.m_149";
$configs{hash_table} = "$configs{database}/reference/hg19.fa.k_21";
}
foreach my $samp (sort keys %cleanList){
my $sampdir="$outdir/$samp/edico";
makedir($sampdir);
my $infqcsv = "$sampdir/result/01fastqList4edico.csv";
open FQCSV, ">$infqcsv" || die $!;
print FQCSV "RGID,RGSM,RGLB,Lane,RGPL,RGCN,Read1File,Read2File\n";
print FQCSV join ("\n", @{$cleanList{$samp}}), "\n";
close FQCSV;
&RunEdico($samp, $infqcsv);
}
}
## fastq Clean by SOAPnuke
sub peLaneClean{
my ($samp,$laneIndex,$dir,$fq1,$fq2,$cleanfq1,$cleanfq2)=@_;
my $cleandir=dirname $cleanfq1;
my $cleanfq1name = basename $cleanfq1;
my $cleanfq2name = basename $cleanfq2;
my $shell1="$dir/shell/clean.sh";
my $cleanCmd="if [ -e \"$cleanfq1\" ];then rm -rf $cleanfq1;fi && \\\n";
$cleanCmd.="if [ -e \"$cleanfq2\" ];then rm -rf $cleanfq2;fi && \\\n";
$cleanCmd.="$configs{SOAPnuke} filter -1 $fq1 -2 $fq2 $configs{SOAPnukePara} -o $cleandir -C $cleanfq1name -D $cleanfq2name && \\\n";
$cleanCmd.="gzip -t $cleanfq1 $cleanfq2";
generateShell($shell1, $cleanCmd);
my $shell2="$dir/shell/cleanQC.sh";
my $cleanQCCmd.="perl $configs{bin}/soapnuke_stat.pl $cleandir/Basic_Statistics_of_Sequencing_Quality.txt $cleandir/Statistics_of_Filtered_Reads.txt > $cleandir/$laneIndex.clean.stat && \\\n";
#$cleanQCCmd.="rm -rf $cleandir/*.txt && \\\n";
$cleanQCCmd.="$configs{fqcheck} -r $cleanfq1 -c $cleandir/$laneIndex\_1.clean.fqcheck && \\\n";
$cleanQCCmd.="$configs{fqcheck} -r $cleanfq2 -c $cleandir/$laneIndex\_2.clean.fqcheck && \\\n";
$cleanQCCmd.="export GNUPLOT_PS_DIR=/share/app/gnuplot-4.6.7/share/gnuplot/4.6/PostScript && \\\n";
$cleanQCCmd.="perl $configs{bin}/fqcheck_distribute.pl $cleandir/$laneIndex\_1.clean.fqcheck $cleandir/$laneIndex\_2.clean.fqcheck -o $cleandir/$laneIndex.clean.";
generateShell($shell2, $cleanQCCmd);
}
## Alignment by bwa mem
sub BWA_mem{
my ($samp,$cleanfq1,$cleanfq2,$laneIndex,$lib,$rgid,$dir)=@_;
my $shell="$dir/shell/bwa.sh";
my $memCmd="export LD_LIBRARY_PATH=/hwfssz1/ST_PRECISION/PUB/softwares/packages/lib:\$LD_LIBRARY_PATH && \\\n";
$memCmd.= "$configs{bwa} mem $configs{bwaMemPara} -M -R \'\@RG\\tID:$rgid\\tSM:$samp\\tLB:$lib\\tPU:$laneIndex\\tPL:$configs{platformPara}\\tCN:BGI\' $configs{reference} $cleanfq1 $cleanfq2 | $configs{samtools} view -b -S -F 256 -t $configs{reference}.fai -o $dir/result/$laneIndex.bam - && \\\n";
$memCmd.= "$configs{java8} -Xmx6g -XX:-UseGCOverheadLimit -jar $configs{picard} SortSam VALIDATION_STRINGENCY=SILENT TMP_DIR=$dir/result/tmp CREATE_INDEX=true SORT_ORDER=coordinate I=$dir/result/$laneIndex.bam O=$dir/result/$laneIndex.sort.bam";
generateShell($shell, $memCmd);
}
sub BWA_aln{
my ($samp,$cleanfq1,$cleanfq2,$laneIndex,$lib,$rgid,$dir)=@_;
my $shell1="$dir/shell/bwa_aln1.sh";
my $aln1Cmd="$configs{bwa} aln $configs{bwaAlnPara} -f $dir/result/$laneIndex\_1.sai $configs{reference} $cleanfq1";
generateShell($shell1,$aln1Cmd);
my $shell2="$dir/shell/bwa_aln2.sh";
my $aln2Cmd="$configs{bwa} aln $configs{bwaAlnPara} -f $dir/result/$laneIndex\_2.sai $configs{reference} $cleanfq2";
generateShell($shell2,$aln2Cmd);
my $shell3="$dir/shell/bwa.sh";
my $sampeCmd="$configs{bwa} sampe $configs{bwaSamPara} -r \"\@RG\\tID:$rgid\\tSM:$samp\\tLB:$lib\\tPU:$laneIndex\\tPL:$configs{platformPara}\\tCN:BGI\" $configs{reference} $dir/result/$laneIndex\_1.sai $dir/result/$laneIndex\_2.sai $cleanfq1 $cleanfq2 | $configs{samtools} view -b -S -t $configs{reference}.fai - >$dir/result/$laneIndex.bam && \\\n";
$sampeCmd.="$configs{java8} -Xmx6g -XX:-UseGCOverheadLimit -jar $configs{picard} SortSam VALIDATION_STRINGENCY=SILENT TMP_DIR=$dir/result/tmp CREATE_INDEX=true SORT_ORDER=coordinate I=$dir/result/$laneIndex.bam O=$dir/result/$laneIndex.sort.bam";
generateShell($shell3,$sampeCmd);
}
sub laneQC{
my ($samp,$laneIndex,$lib,$dir)=@_;
my $bwabam="$dir/result/$laneIndex.bam";
my $sortbam="$dir/result/$laneIndex.sort.bam";
my $shell="$dir/shell/laneQC.sh";
my $cmd="export LD_LIBRARY_PATH=/hwfssz1/ST_PRECISION/PUB/softwares/packages/lib:\$LD_LIBRARY_PATH && \\\n";
$cmd.="$configs{samtools} flagstat $bwabam > $bwabam.stat && \\\n";
$cmd.="$configs{samtools} flagstat $sortbam > $sortbam.stat && \\\n";
$cmd.="diff1=\$(diff $bwabam.stat $sortbam.stat | wc -l) && \\\n";
$cmd.="[ \$diff1 -eq 0 ] && [ -e $dir/raw/$laneIndex\_1.clean.fq.gz ] && rm $dir/raw/$laneIndex\_1.clean.fq.gz && \\\n";
$cmd.="[ \$diff1 -eq 0 ] && [ -e $dir/raw/$laneIndex\_2.clean.fq.gz ] && rm $dir/raw/$laneIndex\_2.clean.fq.gz && \\\n";
$cmd.="[ \$diff1 -eq 0 ] && [ -e $dir/result/$laneIndex\_1.sai ] && rm $dir/result/$laneIndex\_1.sai && \\\n";
$cmd.="[ \$diff1 -eq 0 ] && [ -e $dir/result/$laneIndex\_2.sai ] && rm $dir/result/$laneIndex\_2.sai && \\\n";
$cmd.="[ \$diff1 -eq 0 ] && [ -e $bwabam ] && rm -r $bwabam $dir/result/tmp";
generateShell($shell,$cmd);
}
# from /hwfssz1/ST_CANCER/POL/SHARE/CancerPipeline/Edico_BGISEQ_v1.0/CancerPipeline_Edico_BGISEQ_v1.0.pl
sub RunEdico{
my ($samp, $infqcsv) = @_;
my $sampdir="$outdir/$samp/edico";
my $tmpdir = "$sampdir/tmp";
my $addpara = "--dbsnp=$configs{GATKdbsnp150}";
if ($configs{seqType} =~ /WES/i){ # WES & Panel
$addpara= " --vc-target-bed=$outdir/TR/CallVariantRegion/ex_region.sort.bed";
}
open ECSH, ">$sampdir/shell/edico.$samp.sh" || die $!;
print ECSH "#!/bin/bash\necho ==========start at : `date` ==========\n";
print ECSH "/opt/edico/bin/dragen_reset && \\\n";
#print ECSH "if [ ! -d \"$tmpdir\" ];then mkdir $tmpdir;fi && \\\n";
print ECSH "/opt/edico/bin/dragen --force --ref-dir=$configs{hash_table} --fastq-list=$infqcsv --fastq-list-sample-id=$samp --output-file-prefix=$samp.mkdup --output-directory=$sampdir/result --intermediate-results-dir=$tmpdir --output-format=bam --enable-map-align-output=true --enable-sort=true --enable-duplicate-marking=true --enable-bam-indexing=true --enable-variant-caller=true --vc-emit-ref-confidence=GVCF --vc-reference=$configs{reference} --vc-sample-name=$samp $addpara && \\\n";
print ECSH "/opt/edico/bin/dragen --ref-dir=$configs{hash_table} --variant=$sampdir/result/$samp.mkdup.hard-filtered.gvcf.gz --output-file-prefix=$samp.mkdup.joint --output-directory=$sampdir/result --intermediate-results-dir=$tmpdir --enable-joint-genotyping=true $addpara && \\\n";
#print ECSH "rm -rf $sampdir/result/$samp.mkdup.gvcf.gz* $sampdir/result/$samp.mkdup.joint.vcf.gz* && \\\n";
print ECSH "if [ -d \"$tmpdir\" ];then rm -rf $tmpdir;fi && \\\n";
print ECSH echostring("$sampdir/shell/edico.$samp.sh");
close ECSH;
my $shell1="$sampdir/shell/edico_process.sh";
my $cmd1.= "/usr/bin/gunzip $sampdir/result/$samp.mkdup.gvcf.gz && \\\n";
$cmd1.= << "HERE";
sed -i '2s#this location">#this location">\\n\\#\\#FILTER=<ID=LowQual,Description="Low quality">#' $sampdir/result/$samp.mkdup.gvcf && \\
HERE
$cmd1.="$configs{bin}/bgzip $sampdir/result/$samp.mkdup.gvcf && \\\n";
$cmd1.="$configs{bin}/tabix -p vcf $sampdir/result/$samp.mkdup.gvcf.gz && \\\n";
$cmd1.="ln -s $sampdir/result/$samp.mkdup.gvcf.gz $outdir/$samp/gatkGVCF/raw/$samp.gvcf.gz && \\\n";
$cmd1.="ln -s $sampdir/result/$samp.mkdup.gvcf.gz.tbi $outdir/$samp/gatkGVCF/raw/$samp.gvcf.gz.tbi && \\\n";
$cmd1.="mkdir -p $outdir/$samp/rmdup/result && \\\n";
$cmd1.="ln -s $sampdir/result/$samp.mkdup.bam $outdir/$samp/rmdup/result/$samp.rmdup.bam && \\\n";
$cmd1.="ln -s $sampdir/result/$samp.mkdup.bam.bai $outdir/$samp/rmdup/result/$samp.rmdup.bam.bai";
generateShell($shell1,$cmd1);
open ANNSH, ">$sampdir/shell/variant_Ann.$samp.sh" || die $!;
print ANNSH "#!/bin/bash\necho ==========start at : `date` ==========\n";
print ANNSH "export LD_LIBRARY_PATH=/hwfssz1/ST_CANCER/POL/SHARE/tools/samtools/xz/v5.2.3/lib:\$LD_LIBRARY_PATH && \\\n";
print ANNSH "zcat $sampdir/result/$samp.mkdup.joint.hard-filtered.vcf.gz | awk '\$1~/^#/ || \$7==\"PASS\" {print}' | $configs{bcftools} norm -m-both | $configs{bcftools} norm -f $configs{reference} -o $sampdir/result/$samp.mkdup.joint.hard-filtered.PASS.vcf.gz -O z && \\\n";
print ANNSH "perl $configs{annovar}/table_annovar.pl $sampdir/result/$samp.mkdup.joint.hard-filtered.PASS.vcf.gz $configs{annovardb} -buildver hg19 -out $sampdir/result/$samp.mkdup.joint.hard-filtered.PASS -remove -protocol refGene,ensGene,knownGene,cytoBand,genomicSuperDups,gwasCatalog,phastConsElements100way,phastConsElements46way,targetScanS,tfbsConsSites,wgRna,avsnp147,clinvar_20170130,cosmic82_coding,cosmic82_noncoding,dbnsfp33a,exac03nonpsych,exac03nontcga,gnomad_exome,gnomad_genome,popfreq_all_20150413,intervar_20170202 -operation g,g,g,r,r,r,r,r,r,r,r,f,f,f,f,f,f,f,f,f,f,f -nastring . -vcfinput && \\\n";
print ANNSH "gzip -f $sampdir/result/$samp.mkdup.joint.hard-filtered.PASS.hg19_multianno.txt && \\\n";
print ANNSH "rm -rf $sampdir/result/$samp.mkdup.joint.hard-filtered.PASS.vcf.gz $sampdir/result/$samp.mkdup.joint.hard-filtered.PASS.hg19_multianno.vcf $sampdir/result/$samp.mkdup.joint.hard-filtered.PASS.avinput && \\\n";
print ANNSH echostring("$sampdir/shell/variant_Ann.$samp.sh");
close ANNSH;
# read count per 1000bp
open RCSH, ">$sampdir/shell/bam_readcount.$samp.sh" || die $!;
print RCSH "#!/bin/bash\necho ==========start at : `date` ==========\n";
if (`grep chr $configs{reference}\.fai`){
print RCSH "$configs{bin}/readCounter -w 1000 -c chr1,chr2,chr3,chr4,chr5,chr6,chr7,chr8,chr9,chr10,chr11,chr12,chr13,chr14,chr15,chr16,chr17,chr18,chr19,chr20,chr21,chr22,chrX,chrY $sampleBams{$samp} | gzip > $sampdir/result/$samp.mkdup.win1k.wig.gz && \\\n";
}
else{
print RCSH "$configs{bin}/readCounter -w 1000 -c 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,X,Y $sampleBams{$samp} | gzip > $sampdir/result/$samp.mkdup.win1k.wig.gz && \\\n";
}
print RCSH echostring("$sampdir/shell/bam_readcount.$samp.sh");
close RCSH;
# calculate the quality control values used in the ICGC PanCan project. Generally for WGS data.
# modify form https://github.com/eilslabs/PanCanQC
my $qcdir = "$outdir/$samp/QC";
my $ACEseq = "$outdir/$samp/QC/ACEseq";
my $qcshell = "$outdir/$samp/QC/shell";
unless(-e $qcdir){system("mkdir -p $qcdir")}
unless(-e $ACEseq){system("mkdir -p $ACEseq")}
unless(-e $qcshell){system("mkdir -p $qcshell")}
open STEP1, ">$qcshell/flagstat.$samp.sh" || die $!;
print STEP1 "#!/bin/bash\necho ==========start at : `date` ==========\n";
print STEP1 "export LD_LIBRARY_PATH=/hwfssz1/ST_CANCER/POL/SHARE/tools/samtools/xz/v5.2.3/lib:\$LD_LIBRARY_PATH && \\\n";
print STEP1 "$configs{samtools} flagstat $sampleBams{$samp} > $qcdir/$samp.flagstat && \\\n";
print STEP1 echostring("$qcshell/flagstat.$samp.sh");
close STEP1;
open STEP2, ">$qcshell/coverageQc.$samp.sh" || die $!;
print STEP2 "#!/bin/bash\necho ==========start at : `date` ==========\n";
print STEP2 "$configs{PanCanQC}/coverageQc --alignmentFile=$sampleBams{$samp} --outputFile=$qcdir/$samp.genome_coverage --processors=1 --basequalCutoff=0 --ungappedSizes=$configs{PanCanQC}/hg19.chrLenOnlyACGT_realChromosomes.tab && \\\n";
print STEP2 echostring("$qcshell/coverageQc.$samp.sh");
close STEP2;
open STEP3, ">$qcshell/genomeCoverage.$samp.sh" || die $!;
print STEP3 "#!/bin/bash\necho ==========start at : `date` ==========\n";
print STEP3 "$configs{PanCanQC}/genomeCoverage --alignmentFile=$sampleBams{$samp} --outputFile=/dev/stdout --processors=4 --mode=countReads --windowSize=1 | perl $configs{PanCanQC}/filter_readbins.pl - $configs{PanCanQC}/hg19.chrLenOnlyACGT_realChromosomes.tab | gzip > $qcdir/$samp.readbin_coverage.gz && \\\n";
print STEP3 "gzip -dc $qcdir/$samp.readbin_coverage.gz | awk '{print \$1,\$2,\$2+999,\$3}' | sed 's/ /\\t/g' | sed '1i\\#chr\\tpos\\tend\\tcoverage' | perl $configs{PanCanQC}/annotate_vcf.pl -a - --aFileType=custom --aChromColumn chr --aPosColumn pos --aEndColumn end -b $configs{PanCanQC}/wgEncodeCrgMapabilityAlign100mer_chr.bedGraph.gz --tabix_bin /$configs{bin}/tabix --bFileType=bed --reportBFeatCoord --columnName map | $configs{pythonBin}/python $configs{PanCanQC}/addMappability.py -o $ACEseq/$samp.readbin_coverage.Mappability.gz && \\\n";
print STEP3 "$configs{pythonBin}/python $configs{PanCanQC}/merge_and_filter_cnv.py --inputfile $ACEseq/$samp.readbin_coverage.Mappability.gz --output $ACEseq/$samp.readbin_coverage.Mappability.filtered.gz --coverage 0 --mappability 1000 --NoOfWindows 5 && \\\n";
print STEP3 "/share/app/R-3.3.2/bin/Rscript $configs{PanCanQC}/correctGCBias.R --windowFile $ACEseq/$samp.readbin_coverage.Mappability.filtered.gz --timefile $configs{PanCanQC}/ReplicationTime_10cellines_mean_10KB.Rda --chrLengthFile $configs{PanCanQC}/chrlengths.txt --pid $samp --outfile $samp.corrected.txt --corPlot $samp.gc_corrected.png --corTab $samp.qc_gc_corrected.tsv --qcTab $samp.qc_gc_corrected.slim.txt --gcFile $configs{PanCanQC}/hg19_GRch37_100genomes_gc_content_10kb.txt --outDir $ACEseq --lowess_f 0.1 --scaleFactor 0.9 --coverageYlims 4 && \\\n";
print STEP3 echostring("$qcshell/genomeCoverage.$samp.sh");
close STEP3;
open STEP4, ">$qcshell/bam_stats.$samp.sh" || die $!;
print STEP4 "#!/bin/bash\necho ==========start at : `date` ==========\n";
print STEP4 "/share/app/glibc-2.17/lib/ld-2.17.so --library-path /share/app/glibc-2.17/lib:/share/app/libz/zlib-1.2.11 $configs{PanCanQC}/bam_stats -i $sampleBams{$samp} -o $qcdir/$samp.read_edits && \\\n";
print STEP4 echostring("$qcshell/bam_stats.$samp.sh");
close STEP4;
open STEP5, ">$qcshell/summary.$samp.sh" || die $!;
print STEP5 "#!/bin/bash\necho ==========start at : `date` ==========\n";
print STEP5 "perl $configs{PanCanQC}/summaryQC.pl $qcdir/$samp.flagstat $qcdir/$samp.genome_coverage $ACEseq/$samp.qc_gc_corrected.slim.txt $qcdir/$samp.read_edits $qcdir/$samp.readbin_coverage.gz $configs{PanCanQC}/hg19_reducedGenome.n300l5M.sorted.bed $qcdir/$samp.PanCanQC.summary.txt && \\\n";
print STEP5 "tar -czf $qcdir/ACEseq.tar.gz $ACEseq && rm -rf $ACEseq && \\\n";
print STEP5 echostring("$qcshell/summary.$samp.sh");
close STEP5;
# remove clean reads
if ($configs{rmCleanReads}=~/true/i){
open RMSH, ">$sampdir/shell/rmCleanReads.$samp.sh" || die $!;
print RMSH "#!/bin/bash\necho ==========start at : `date` ==========\n";
print RMSH "perl $configs{bin}/rm_cleanReads_edico_pipeline.pl $sampleBams{$samp} $infqcsv && \\\n";
print RMSH echostring("$sampdir/shell/rmCleanReads.$samp.sh");
close RMSH;
}
}
## mark duplication and merge
sub MarkDuplicates{
my ($hashref) = @_;
my %bwaBams = %$hashref;
foreach my $samp (sort keys %bwaBams){
my $dir="$outdir/$samp/rmdup";
makedir($dir);
my $shell1="$dir/shell/rmdup.$samp.sh";
my $cmd1="$configs{java8} -Djava.io.tmpdir=$dir/tmp -Xmx6g -XX:-UseGCOverheadLimit -jar $configs{picard} MarkDuplicates CREATE_INDEX=true";
my $bwaBam='';
foreach my $lib (sort keys %{$bwaBams{$samp}}){
foreach my $laneIndex (sort keys %{$bwaBams{$samp}{$lib}}) {
$cmd1.=" I=$bwaBams{$samp}{$lib}{$laneIndex}";
$bwaBam.="$bwaBams{$samp}{$lib}{$laneIndex} ";
}
}
$cmd1.=" O=$dir/result/$samp.rmdup.bam METRICS_FILE=$dir/result/$samp.rmdup.bam.met TMP_DIR=$dir/tmp $configs{picardRmdupPara}";
generateShell($shell1,$cmd1);
my $shell2="$dir/shell/rmdupQC.sh";
my $cmd2.="$configs{samtools} flagstat $dir/result/$samp.rmdup.bam > $dir/result/$samp.rmdup.bam.stat && \\\n";
$cmd2.="$configs{samtools} idxstats $dir/result/$samp.rmdup.bam > $dir/result/$samp.rmdup.bam.idxstats && \\\n";
chop($bwaBam);
$cmd2.="#rm -rf $bwaBam";
generateShell($shell2,$cmd2);
}
}
## Perform local realnment of reads around indels on patient level
sub PatientRealn{
my ($corealnFile)=@_;
makedir("$outdir/Co-realn");
`mkdir -p $outdir/Co-realn/intervals`;
open CORE,"$corealnFile" or die $!;
while(<CORE>){
chomp;
my @RealnSamples=split /\s+/,$_;
foreach my $chr (@chrs){
corealnShell(\@RealnSamples,$chr);
}
}
close CORE;
}
sub GATKrealnRecal{
my ($ref)=@_;
my %rmdupBams = %$ref;
foreach my $samp(sort keys %rmdupBams){
my $rmdupBam=$rmdupBams{$samp};
my $realndir="$outdir/$samp/realn";
makedir($realndir);
my $brecaldir="$outdir/$samp/brecal";
makedir($brecaldir);
foreach my $chr (@chrs){
my $realnBam = $realnBams{$samp}{$chr};
my $brecalBam = $brecalBams{$samp}{$chr};
my $shell="$realndir/shell/GATKrealnRecal.$samp.$chr.sh";
my $cmd= "$configs{java8} -Djava.io.tmpdir=$realndir/tmp -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T RealignerTargetCreator -l INFO -I $rmdupBam -R $configs{reference} -L $outdir/TR/interval/$chr.intervals -o $realndir/raw/$samp.$chr.realn.intervals -known $configs{GATKkgIndel} -known $configs{GATKmillsIndel} -nt 4 && \\\n";
$cmd.= "$configs{java8} -Djava.io.tmpdir=$realndir/tmp -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T IndelRealigner -l INFO -maxReads 60000 -maxInMemory 400000 -I $rmdupBam -R $configs{reference} ";
$cmd.="-L $chr " if($chr ne "allchr");
$cmd.="-o $realnBam -targetIntervals $realndir/raw/$samp.$chr.realn.intervals -known $configs{GATKkgIndel} -known $configs{GATKmillsIndel} && \\\n";
$cmd.="$configs{java8} -Djava.io.tmpdir=$brecaldir/tmp -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T BaseRecalibrator -l INFO -I $realnBam -R $configs{reference} -L $outdir/TR/interval/$chr.intervals -knownSites $configs{GATKdbsnp} -knownSites $configs{GATKkgIndel} -knownSites $configs{GATKmillsIndel} -o $brecaldir/raw/$samp.$chr.brecal.grp -nct 4 && \\\n";
$cmd.="$configs{java8} -Djava.io.tmpdir=$brecaldir/tmp -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T PrintReads -l INFO -I $realnBam -R $configs{reference} -BQSR $brecaldir/raw/$samp.$chr.brecal.grp -o $brecalBam -nct 4 && \\\n";
$cmd.="rm $realnBam";
generateShell($shell,$cmd);
}
}
}
sub corealnShell{
my ($ref,$chr)=@_;
my @RealnSamples=@$ref;
my $patientId= shift @RealnSamples;
my $realIn='';
my @outbamList=();
for my $samp(@RealnSamples){
makedir("$outdir/$samp/realn") if(!-e "$outdir/$samp/realn");
my $rmdupBam=$rmdupBams{$samp};
my $realnBam=$realnBams{$samp}{$chr};
my $rmdupBamName=(split /\//,$rmdupBam)[-1];
push @outbamList,"$rmdupBamName\t$realnBam";
$realIn.="-I $rmdupBam ";
$patientIds{$samp}=$patientId;
}
chop($realIn);
my $sampleNum=scalar(@RealnSamples);
if(!exists $maxmems{$patientId}){
$maxmems{$patientId} = 4 * ($sampleNum/4 + 1) > 10 ? 14 : 10 * ($sampleNum/10 + 1);
$maxmems{$patientId} .= "g";
}
my $maxR = 20000 * $sampleNum > 150000 ? 150000 : 20000 * $sampleNum;
my $maxRiM = 150000 * $sampleNum > 1000000 ? 1000000 : 150000 * $sampleNum;
my $outbam="$outdir/Co-realn/result/$patientId.$chr.co-realn.bam.map";
open COL,">$outbam";print COL join("\n",@outbamList);close COL;
my $shell="$outdir/Co-realn/shell/realn.$patientId.$chr.sh";
my $realnCmd="$configs{java8} -Djava.io.tmpdir=$outdir/Co-realn/tmp -Xmx$maxmems{$patientId} -XX:-UseGCOverheadLimit -jar $configs{GATK} -T RealignerTargetCreator -l INFO $realIn -R $configs{reference} -L $outdir/TR/interval/$chr.intervals -o $outdir/Co-realn/intervals/$patientId.$chr.realn.intervals -known $configs{GATKkgIndel} -known $configs{GATKmillsIndel} -nt 4 && \\\n";
$realnCmd.="$configs{java8} -Djava.io.tmpdir=$outdir/Co-realn/tmp -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T IndelRealigner -l INFO -maxReads $maxR -maxInMemory $maxRiM $realIn -R $configs{reference} ";
$realnCmd.="-L $chr " if($chr ne "allchr");
$realnCmd.="-nWayOut $outbam -targetIntervals $outdir/Co-realn/intervals/$patientId.$chr.realn.intervals -known $configs{GATKkgIndel} -known $configs{GATKmillsIndel}";
foreach(@outbamList){
my $realnBam=(split /\t/,$_)[-1];
$realnCmd.=" && \\\n$configs{samtools} index $realnBam";
}
generateShell($shell,$realnCmd);
}
sub BaseRecalibrator{
my ($hashref) = @_;
my %realnBams = %$hashref;
foreach my $samp(sort keys %realnBams){
my $dir="$outdir/$samp/brecal";
makedir($dir);
foreach my $chr( sort keys %{$realnBams{$samp}}){
my $shell="$dir/shell/brecal.$samp.$chr.sh";
my $brecalCmd="$configs{java8} -Djava.io.tmpdir=$dir/tmp -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T BaseRecalibrator -l INFO -I $realnBams{$samp}{$chr} -R $configs{reference} -L $outdir/TR/interval/$chr.intervals -knownSites $configs{GATKdbsnp} -knownSites $configs{GATKkgIndel} -knownSites $configs{GATKmillsIndel} -o $dir/raw/$samp.$chr.brecal.grp -nct 4 && \\\n";
$brecalCmd.="$configs{java8} -Djava.io.tmpdir=$dir/tmp -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T PrintReads -l INFO -I $realnBams{$samp}{$chr} -R $configs{reference} -BQSR $dir/raw/$samp.$chr.brecal.grp -o $dir/result/$samp.$chr.brecal.bam -nct 4";
generateShell($shell,$brecalCmd);
}
}
}
sub mergeBrecalBAM{
my ($hashref,$hashref_merge) = @_;
my %brecalBams = %$hashref;
my %mergeBams = %$hashref_merge;
foreach my $samp(sort keys %brecalBams){
my $sampleBAM=$sampleBams{$samp};
my $shell="$outdir/$samp/brecal/shell/mergeBam.$samp.sh";
my $mergeCmd = "export LD_LIBRARY_PATH=/hwfssz1/ST_PRECISION/PUB/softwares/packages/lib:\$LD_LIBRARY_PATH && \\\n";
$mergeCmd.="$configs{java8} -Djava.io.tmpdir=$outdir/$samp/brecal/tmp -Xmx2g -XX:-UseGCOverheadLimit -jar $configs{picard} MergeSamFiles CREATE_INDEX=true";
foreach my $chr( sort keys %{$brecalBams{$samp}}){
$mergeCmd.=" I=$brecalBams{$samp}{$chr}";
}
$mergeCmd.=" O=$mergeBams{$samp} SO=coordinate AS=true VALIDATION_STRINGENCY=SILENT && \\\n";
$mergeCmd.="$configs{samtools} flagstat $mergeBams{$samp} > $outdir/$samp/brecal/result/$samp.bam.stat";
generateShell($shell,$mergeCmd);
}
}
## calculate the coverage and depth information for each sampleM
sub bamdstQC{
my ($hashref) = @_;
my %sampleBams = %$hashref;
foreach my $samp(sort keys %sampleBams){
my $dir="$outdir/$samp/coverage";
makedir($dir);
my $shell="$dir/shell/covdep.$samp.sh";
my $cmd="export LD_LIBRARY_PATH=$configs{bin}/bamtools-2.4.1/lib:\$LD_LIBRARY_PATH && \\\n";
$cmd.="$configs{bin}/bamdst $configs{covdepPara} -p $outdir/TR/bed/allchr.bed -o $dir/result $sampleBams{$samp} && \\\n";
$cmd.="perl $configs{bin}/bamdstPlot.pl -i $dir/result/depth_distribution.plot -c $dir/result/coverage.report -o $dir/result && \\\n";
$cmd.="$configs{samtools} view -bu -F 4 $sampleBams{$samp} -L $outdir/TR/bed/allchr.bed | $configs{bin}/bamtools stats -in /dev/stdin -insert > $dir/result/Target.report";
generateShell($shell,$cmd);
}
}
## Analyze coverage distribution and validate read mates per interval and per sample
## Collect quality metrics for a set of intervals
sub TargetQC{
my ($hashref) = @_;
my %sampleBams = %$hashref;
foreach my $samp(sort keys %sampleBams){
my $dir="$outdir/$samp/coverage";
my $bam=$sampleBams{$samp};
my $shell="$dir/shell/targetqc.$samp.sh";
my $targCmd="$configs{java8} -Djava.io.tmpdir=$dir/tmp -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T DiagnoseTargets -l INFO -I $bam -R $configs{reference} -L $outdir/TR/interval/allchr.intervals -o $dir/result/$samp.TqrgetQC.vcf.gz -missing $dir/result/$samp.TqrgetQC.missing.intervals && \\\n";
$targCmd.="$configs{java8} -Djava.io.tmpdir=$dir/tmp -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T QualifyMissingIntervals -l INFO -I $bam -R $configs{reference} -targets $outdir/TR/interval/allchr.intervals -L $dir/result/$samp.TqrgetQC.missing.intervals -o $dir/result/$samp.TqrgetQC.missing.grp";
generateShell($shell,$targCmd);
}
}
sub DNA_damage{
my ($hashref) = @_;
my %sampleBams = %$hashref;
foreach my $samp(sort keys %sampleBams){
my $dir = "$outdir/$samp/DNA_damage";
makedir($dir);
my $bam=$sampleBams{$samp};
my $shell="$dir/shell/DNA_damage.$samp.sh";
my $Cmd = "perl $configs{bin}/Damage-estimator.pl $bam $configs{reference} $dir/result $samp";
generateShell($shell,$Cmd);
}
}
## calling SNP and INDEL by GATK34
sub gatkDISCOVERY{
my ($samp,$bam,$chr)=@_;
my $dir="$outdir/$samp/gatkDISCOVERY";
my $shell1 = "$dir/shell/variant.$samp.$chr.sh";
my $gatkCmd="$configs{java8} -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T HaplotypeCaller -l INFO -R $configs{reference} -I $bam -L $outdir/TR/CallVariantRegion/ex_region.sort.$chr.bed --genotyping_mode DISCOVERY -stand_call_conf 30 -o $dir/raw/$samp.$chr.HC.variant.vcf.gz && \\\n";
$gatkCmd.="$configs{java8} -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T UnifiedGenotyper -l INFO -R $configs{reference} -I $bam -L $outdir/TR/CallVariantRegion/ex_region.sort.$chr.bed --genotyping_mode DISCOVERY -stand_call_conf 30 -o $dir/raw/$samp.$chr.UG.variant.vcf.gz";
generateShell($shell1,$gatkCmd);
#hard-filtering for SNP (single WES unable to use VQSR)
my $shell2="$dir/shell/variant.$samp.$chr.process.sh";
my $gatkFiltcmd="$configs{java8} -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T SelectVariants -l INFO -V $dir/raw/$samp.$chr.UG.variant.vcf.gz -R $configs{reference} -selectType SNP -o $dir/raw/$samp.$chr.snp_raw.vcf.gz && \\\n";
$gatkFiltcmd.="$configs{java8} -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T VariantFiltration -l INFO -V $dir/raw/$samp.$chr.snp_raw.vcf.gz -R $configs{reference} -filter \"QD < 2.0 || FS > 60.0 || MQ < 40.0 || SOR > 4.0 || MQRankSum < -12.5 || ReadPosRankSum < -8.0\" -filterName \"SNP_filter\" -o $dir/raw/$samp.$chr.snp_filter.vcf.gz && \\\n";
#Vhard-filtering for INDEL
$gatkFiltcmd.="$configs{java8} -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T SelectVariants -l INFO -V $dir/raw/$samp.$chr.HC.variant.vcf.gz -R $configs{reference} -selectType INDEL -o $dir/raw/$samp.$chr.indel_raw.vcf.gz && \\\n";
$gatkFiltcmd.="$configs{java8} -Xmx4g -XX:-UseGCOverheadLimit -jar $configs{GATK} -T VariantFiltration -l INFO -V $dir/raw/$samp.$chr.indel_raw.vcf.gz -R $configs{reference} -filter \"QD < 2.0 || FS > 200.0 || ReadPosRankSum < -20.0 || SOR > 10.0\" -filterName \"INDEL_filter\" -o $dir/raw/$samp.$chr.indel_filter.vcf.gz && \\\n";
#annovar
$gatkFiltcmd.="zcat $dir/raw/$samp.$chr.snp_filter.vcf.gz | awk '\$1~/^#/ || \$7==\"PASS\" {print}' | $configs{bcftools} norm -m-both -f $configs{reference} - | $configs{bcftools} norm -f $configs{reference} -o $dir/result/$samp.$chr.snp.vcf.gz -O z - && \\\n";
$gatkFiltcmd.="zcat $dir/raw/$samp.$chr.indel_filter.vcf.gz | awk '\$1~/^#/ || \$7==\"PASS\" {print}' | $configs{bcftools} norm -m-both -f $configs{reference} - | $configs{bcftools} norm -f $configs{reference} -o $dir/result/$samp.$chr.indel.vcf.gz -O z - && \\\n";
# $gatkFiltcmd.="perl $configs{annovar}/table_annovar.pl $dir/result/$samp.$chr.snp.vcf.gz $configs{annovardb} -buildver hg19 -out $dir/result/$samp.$chr.snp $configs{annovarPara} && \\\n";
# $gatkFiltcmd.="perl $configs{annovar}/table_annovar.pl $dir/result/$samp.$chr.indel.vcf.gz $configs{annovardb} -buildver hg19 -out $dir/result/$samp.$chr.indel $configs{annovarPara} && \\\n";
# $gatkFiltcmd.="gzip -f $dir/result/$samp.$chr.snp.hg19_multianno.txt $dir/result/$samp.$chr.indel.hg19_multianno.txt && \\\n";
# $gatkFiltcmd.="rm -rf $dir/raw/$samp.$chr.snp_raw.vcf.gz* $dir/raw/$samp.$chr.indel_raw.vcf.gz* && \\\n";
# $gatkFiltcmd.="rm -rf $dir/result/$samp.$chr.snp.avinput $dir/result/$samp.$chr.snp.hg19_multianno.vcf $dir/result/$samp.$chr.indel.avinput $dir/result/$samp.$chr.indel.hg19_multianno.vcf";
generateShell($shell2,$gatkFiltcmd);
}
sub gatkGVCF_sample{
my ($samp,$bam,$chr)=@_;
my $dir = "$outdir/$samp/gatkGVCF";
my $shell = "$dir/shell/gatkGVCF.$samp.$chr.sh";
my $cmd;
if ($configs{seqType} =~ /WES/i){
$cmd="$configs{java8} -Xmx5G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T HaplotypeCaller -R $configs{reference} -I $bam -L $outdir/TR/CallVariantRegion/ex_region.sort.$chr.bed --emitRefConfidence GVCF -o $dir/raw/$samp.$chr.g.vcf.gz && \\\n";
}elsif ($configs{seqType} =~ /WES/i){
$cmd="$configs{java8} -Xmx5G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T HaplotypeCaller -R $configs{reference} -I $bam -L $chr --emitRefConfidence GVCF -o $dir/raw/$samp.$chr.g.vcf.gz && \\\n";
}
$cmd.="$configs{java8} -Xmx2G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T GenotypeGVCFs -R $configs{reference} --variant $dir/raw/$samp.$chr.g.vcf.gz -o $dir/result/$samp.$chr.vcf.gz -stand_call_conf 30 -allSites";
generateShell($shell,$cmd);
}
sub gatkGVCF_GenotypeGVCFs{
my ($samp)=@_;
my $dir = "$outdir/$samp/gatkGVCF";
my $shell = "$dir/shell/gatkGVCF.$samp.sh";
my $cmd="$configs{java8} -Xmx2G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T GenotypeGVCFs -R $configs{reference} --variant $dir/raw/$samp.gvcf.gz -o $dir/result/$samp.vcf.gz -stand_call_conf 30 -allSites";
generateShell($shell,$cmd);
}
sub gatkGVCF_merge{
my ($samp) = @_;
my $dir = "$outdir/$samp/gatkGVCF";
my $shell1 = "$dir/shell/gatkGVCF.$samp.merge.sh";
my $cmd1 = "$configs{java8} -cp $configs{GATK} org.broadinstitute.gatk.tools.CatVariants -R $configs{reference}";
for my $chr (@chrs){
$cmd1 .= " -V $dir/result/$samp.$chr.vcf.gz";
}
$cmd1 .= " -out $dir/result/$samp.vcf.gz --assumeSorted";
generateShell($shell1,$cmd1);
}
sub gatkGVCF_process{
my ($samp) = @_;
my $dir = "$outdir/$samp/gatkGVCF";
my $shell2 = "$dir/shell/gatkGVCF.$samp.SNP.sh";
my $cmd2 = "export PERL5LIB=\"$configs{lib}:\$PERL5LIB\"\n";
$cmd2 .= "export LIBRARY_PATH=\"/hwfssz1/ST_PRECISION/PUB/softwares/packages/lib:\$LIBRARY_PATH\"\n";
$cmd2 .= "$configs{java8} -Xmx3G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T SelectVariants -R $configs{reference} -V $dir/result/$samp.vcf.gz -selectType SNP --excludeNonVariants -o $dir/raw/$samp.raw.snp.vcf.gz && \\\n";
$cmd2 .= "$configs{java8} -Xmx3G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T VariantFiltration -R $configs{reference} \\\n";
$cmd2 .= "-V $dir/raw/$samp.raw.snp.vcf.gz \\\n";
$cmd2 .= "--filterExpression \"QD < 2.0 || FS > 60.0 || MQ <40.0 || MQRankSum < -12.5 || ReadPosRankSum < -8.0\" --filterName \"filter\" -o $dir/raw/$samp.filtered_snp.vcf.gz && \\\n";
$cmd2 .= "$configs{bcftools} view -e 'ALT==\"*\"' -f \"PASS\" -o $dir/result/$samp.filtered_snp.vcf.gz -O z $dir/raw/$samp.filtered_snp.vcf.gz && \\\n";
$cmd2 .= "$configs{bin}/annodb/annodb.pl --mode snp --if vcf --optdb \'Conservative,Cancer,Disease,Functional,ENCODE,Population\' --genedb \'proteinatlas,omim,pharmgkb,dbnsfp,cgc\' --remove --verdbsnp v149_hg19 --buildver hg19 --outfile $dir/result/$samp.filtered_snp.vcf $dir/result/$samp.filtered_snp.vcf.gz && \\\n";
$cmd2 .= "perl $configs{bin}/annodb/Annodb_stat_for_all.pl -i $dir/result/$samp.filtered_snp.vcf.genome_summary.csv -o $dir/result/$samp.snp.stat -s $samp -v snp && \\\n";
$cmd2 .= "gzip $dir/result/$samp.filtered_snp.vcf.genome_summary.csv $dir/result/$samp.filtered_snp.vcf.exome_summary.csv $dir/result/$samp.filtered_snp.vcf.gene_summary.csv";
generateShell($shell2,$cmd2);
my $shell3 = "$dir/shell/gatkGVCF.$samp.INDEL.sh";
my $cmd3 = "export PERL5LIB=\"$configs{lib}:\$PERL5LIB\"\n";
$cmd3 .= "export LIBRARY_PATH=\"/hwfssz1/ST_PRECISION/PUB/softwares/packages/lib:\$LIBRARY_PATH\"\n";
$cmd3 .= "$configs{java8} -Xmx3G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T SelectVariants -R $configs{reference} -V $dir/result/$samp.vcf.gz -selectType INDEL --excludeNonVariants -o $dir/raw/$samp.raw.indel.vcf.gz && \\\n";
$cmd3 .= "$configs{java8} -Xmx3G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T VariantFiltration -R $configs{reference} \\\n";
$cmd3 .= "-V $dir/raw/$samp.raw.indel.vcf.gz \\\n";
$cmd3 .= "--filterExpression \"QD < 2.0 || FS > 200.0 || ReadPosRankSum < -20.0\" --filterName \"filter\" -o $dir/raw/$samp.filtered_indel.vcf.gz && \\\n";
$cmd3 .= "$configs{bcftools} view -f \"PASS\" -o $dir/result/$samp.filtered_indel.vcf.gz -O z $dir/raw/$samp.filtered_indel.vcf.gz && \\\n";
$cmd3 .= "$configs{bin}/annodb/annodb.pl --mode indel --if vcf --optdb \'Conservative,Cancer,Disease,Functional,ENCODE,Population\' --genedb \'proteinatlas,omim,pharmgkb,dbnsfp,cgc\' --remove --verdbsnp v149_hg19 --buildver hg19 --outfile $dir/result/$samp.filtered_indel.vcf $dir/result/$samp.filtered_indel.vcf.gz && \\\n";
$cmd3 .= "perl $configs{bin}/annodb/Annodb_stat_for_all.pl -i $dir/result/$samp.filtered_indel.vcf.genome_summary.csv -o $dir/result/$samp.indel.stat -s $samp -v indel && \\\n";
$cmd3 .= "perl $configs{bin}/indel_stat.pl $dir/result/$samp.filtered_indel.vcf.genome_summary.csv $dir/result/$samp.indel_len && \\\n";
$cmd3 .= "perl $configs{bin}/indel_stat.pl $dir/result/$samp.filtered_indel.vcf.exome_summary.csv $dir/result/$samp.indel_cds_len && \\\n";
$cmd3 .= "perl $configs{bin}/indel_lenght_R.pl $samp $dir/result/$samp.indel_len.xls $dir/result/$samp.indel_cds_len.xls $dir/result/ && \\\n";
$cmd3 .= "gzip $dir/result/$samp.filtered_indel.vcf.genome_summary.csv $dir/result/$samp.filtered_indel.vcf.exome_summary.csv $dir/result/$samp.filtered_indel.vcf.gene_summary.csv";
generateShell($shell3,$cmd3);
}
sub gatkGVCF_VQSR{
my ($sample) = @_;
my $dir = "$outdir/$sample/gatkGVCF";
my $shell1 = "$dir/shell/gatkGVCF.SNP.VQSR.sh";
my $cmd1 = "export PERL5LIB=\"$configs{lib}:\$PERL5LIB\"\n";
$cmd1 .= "$configs{java8} -Xmx3G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T SelectVariants -R $configs{reference} -V $dir/result/$sample.vcf.gz -selectType SNP --excludeNonVariants -o $dir/raw/$sample.raw.snp.vcf.gz && \\\n";
$cmd1 .= "$configs{java8} -Xmx5G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T VariantRecalibrator -R $configs{reference} -input $dir/raw/$sample.raw.snp.vcf.gz \\\n";
$cmd1 .= "-resource:hapmap,known=false,training=true,truth=true,prior=15.0 $configs{GATKhapmap} \\\n";
$cmd1 .= "-resource:omni,known=false,training=true,truth=true,prior=12.0 $configs{GATKomni} \\\n";
$cmd1 .= "-resource:1000G,known=false,training=true,truth=false,prior=10.0 $configs{GATKkgSNP} \\\n";
$cmd1 .= "-resource:dbsnp,known=true,training=false,truth=false,prior=2.0 $configs{GATKdbsnp} \\\n";
# SNP specific recommendations for Exome data (not use DP)
$cmd1 .= "-an QD -an MQ -an MQRankSum -an ReadPosRankSum -an FS -an SOR -an InbreedingCoeff \\\n";
$cmd1 .= "-mode SNP -tranche 100.0 -tranche 99.9 -tranche 99.0 -tranche 90.0 \\\n";
$cmd1 .= "-recalFile $dir/raw/$sample.recalibrate_SNP.recal -tranchesFile $dir/raw/$sample.recalibrate_SNP.tranches -rscriptFile $dir/raw/VQSR_SNP_plots.R && \\\n";
$cmd1 .= "$configs{java8} -Xmx5G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T ApplyRecalibration -R $configs{reference} -input $dir/raw/$sample.raw.snp.vcf.gz -mode SNP --ts_filter_level 99.0 -recalFile $dir/raw/$sample.recalibrate_SNP.recal -tranchesFile $dir/raw/$sample.recalibrate_SNP.tranches -o $dir/raw/$sample.filtered_snp.vcf.gz && \\\n";
$cmd1 .= "$configs{java8} -Xmx10G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T SelectVariants -R $configs{reference} -V $dir/raw/$sample.filtered_snp.vcf.gz --excludeFiltered -o $dir/result/$sample.filtered_snp.vcf.gz";
generateShell($shell1,$cmd1);
my $shell2 = "$dir/shell/gatkGVCF.Indel.VQSR.sh";
my $cmd2 = "export PERL5LIB=\"$configs{lib}:\$PERL5LIB\"\n";
$cmd2 .= "$configs{java8} -Xmx5G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T SelectVariants -R $configs{reference} -V $dir/result/$sample.vcf.gz -selectType INDEL --excludeNonVariants -o $dir/raw/$sample.raw.indel.vcf.gz && \\\n";
$cmd2 .= "$configs{java8} -Xmx5G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T VariantRecalibrator -R $configs{reference} -input $dir/raw/$sample.raw.indel.vcf.gz \\\n";
$cmd2 .= "-resource:mills,known=true,training=true,truth=true,prior=12.0 $configs{GATKmillsIndel} \\\n";
$cmd2 .= "-resource:dbsnp,known=true,training=false,truth=false,prior=2.0 $configs{GATKdbsnp} \\\n";
$cmd2 .= "-an QD -an FS -an SOR -an ReadPosRankSum -an MQRankSum -an InbreedingCoeff -mode INDEL \\\n";
$cmd2 .= "-tranche 100.0 -tranche 99.9 -tranche 99.0 -tranche 90.0 --maxGaussians 4 \\\n";
$cmd2 .= "-recalFile $dir/raw/$sample.recalibrate_INDEL.recal -tranchesFile $dir/raw/$sample.recalibrate_INDEL.tranches -rscriptFile $dir/raw/$sample.recalibrate_INDEL_plots.R && \\\n";
$cmd2 .= "$configs{java8} -Xmx5G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T ApplyRecalibration -R $configs{reference} -input $dir/raw/$sample.raw.indel.vcf.gz -mode INDEL --ts_filter_level 99.0 -recalFile $dir/raw/$sample.recalibrate_INDEL.recal -tranchesFile $dir/raw/$sample.recalibrate_INDEL.tranches -o $dir/raw/$sample.filtered_indel.vcf.gz && \\\n";
$cmd2 .= "$configs{java8} -Xmx5G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T SelectVariants -R $configs{reference} -V $dir/raw/$sample.filtered_indel.vcf.gz --excludeFiltered -o $dir/result/$sample.filtered_indel.vcf.gz";
generateShell($shell2,$cmd2);
}
sub gatkGVCF_combine_bychr{
my ($dir) = @_;
for my $chr (@chrs){
my $cmd="$configs{java8} -Xmx2G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T GenotypeGVCFs -R $configs{reference}";
foreach my $samp(sort keys %brecalBams){
$cmd.=" --variant $outdir/$samp/gatkGVCF/raw/$samp.$chr.g.vcf.gz";
}
$cmd.=" -o $dir/result/combine.$chr.vcf.gz";
my $shell = "$dir/shell/gatkGVCF.combine.$chr.sh";
generateShell($shell,$cmd);
}
}
sub gatkGVCF_combine_bysample{
my ($dir) = @_;
my $cmd="$configs{java8} -Xmx2G -Djava.io.tmpdir=$dir/tmp -jar $configs{GATK} -T GenotypeGVCFs -R $configs{reference} \\\n";
foreach my $samp(sort keys %brecalBams){
$cmd.="--variant $outdir/$samp/gatkGVCF/result/$samp.gvcf.gz \\\n";
}
$cmd.=" -o $dir/result/combine.vcf.gz";
my $shell = "$dir/shell/gatkGVCF.combine.sh";
generateShell($shell,$cmd);
}
sub gatkGVCF_combineGTs{
my ($ref1,$ref2,$outdir) = @_;
my %patients2samples = %$ref1;
my %configs = %$ref2;