-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclinsv
executable file
·1600 lines (1152 loc) · 63.7 KB
/
clinsv
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
########################################
######################################## modules
########################################
use Getopt::Long;
use File::Path qw(make_path remove_tree);
use File::Basename;
use Cwd;use Cwd "abs_path";
use POSIX qw/strftime/;
use FindBin qw($RealBin);
use lib "$RealBin/../perlib";
use JSON::Parse 'read_json';
use Data::Dumper::Simple;
########################################
######################################## default settings
########################################
$clinSvVersion="1.1.0 (2024-03-08)";
$runString="all";
$inputAlignDir="./input/*.bam";
$lumpyBatchSize=5;
$removeTmpFilesSh=1;
$S_AskBeforeSub=0;
$S_clinsv_dir=dirname(dirname(abs_path($0)));
$S_ref_data="$S_clinsv_dir/refdata-b37";
$projectDir_host='';
$S_ref_data_host='';
# $resource_available_path="$S_clinsv_dir/resource_available.json";
$resource_available_path="";
GetOptions (
"p=s" => \$projectDir,
"r=s" => \$runString,
"i=s" => \$inputAlignDir,
"s=s" => \$sampleInfoFile,
"n=s" => \$nameStemJoint,
"f" => \$force,
"a" => \$S_AskBeforeSub,
"ref=s" => \$S_ref_data,
"hg19" => \$isHg19,
"h" => \$help,
"l=i" => \$lumpyBatchSize,
"eval" => \$eval,
"w" => \$igvweb,
"j=s" => \$resource_available_path,
"v" => \$version,
) or die("Error in command line arguments\n");
if($help){ printHelp(); exit; }
if($version){ print "$clinSvVersion\n"; exit; }
if (length($resource_available_path) < 1){
# Use default resources
$resource_available = use_default_resources();
}else{
# read in json of resources available
$resource_available = read_json($resource_available_path);
# Checks that the resource_availble json contains keys for all required steps
check_resource_available_keys($resource_available);
}
# If no project dir is given default to cwd
if (length($projectDir)<1){
$projectDir=cwd();
}else{
$projectDir = abs_path $projectDir;
}
## check projectDir
@bamInArr=glob("$inputAlignDir");
if(scalar(@bamInArr)==0){print {STDERR} "\n **** error: no bam files found under $inputAlignDir **** \n"; printHelp(); exit}
if ($projectDir =~ /\:/){ ($projectDir_host,$projectDir)=split(":",$projectDir)}
if ($S_ref_data =~ /\:/){ ($S_ref_data_host,$S_ref_data)=split(":",$S_ref_data)}
if ($S_ref_data =~ /refdata-b37/){
$S_ref_data_v='b37';
}elsif ($S_ref_data =~ /refdata-b38/){
$S_ref_data_v='b38';
}else{
print "refdata-bX not matching\n";
exit (1);
}
if (length($projectDir)<1){$projectDir=cwd();}
make_path($projectDir) if (! -d $projectDir);
if (! -d $projectDir){print {STDERR} "\n **** error: project dir $projectDir does not exist and could not be created **** \n"; printHelp(); exit}
$projectDir=~ s/\/$//g;
$projectDirName=basename($projectDir);
$nameStemJoinF=(length($nameStemJoint)>0)? "joined_$nameStemJoint":"joined";
$nameStemJoint="$projectDirName" unless length($nameStemJoint)>0;
if(length($sampleInfoFile)>0){
$readWriteSampleInfo="(read only)";
}else{
$readWriteSampleInfo="";
$sampleInfoFile="$projectDir/sampleInfo.txt";
}
###################### prepare main sh script header
print "##############################################\n";
print "#### ClinSV ####\n";
print "##############################################\n";
print "# ".strftime("%d/%m/%Y\ %H:%M:%S",localtime)."\n\n";
print "# clinsv dir: $S_clinsv_dir\n";
print "# projectDir: $projectDir\n";
print "# sampleInfoFile: $sampleInfoFile $readWriteSampleInfo\n";
print "# name stem: $nameStemJoint\n";
print "# lumpyBatchSize: $lumpyBatchSize\n";
print "# genome reference: $S_ref_data\n";
print "# run steps: $runString\n";
print "# number input bams: ".scalar(@bamInArr)."\n";
print "# qc eval\n" if $eval;
print "# use web IGV links to annotation tracks\n" if $igvweb;
print "\n";
####################### prepare resource vars
# for tabix, samtools, bgzip etc..
$softBin="$S_clinsv_dir/bin";
# CLinSV scripts
$S_SV_scriptDir="$S_clinsv_dir/clinSV/scripts";
# python
#$S_python="module load python/2.7.3\nsource /path/virtenv.a1/bin/activate\n"; # if not already in path
# ref-data resource
$S_SV_controlSRPE="$S_ref_data/control/brkp";
$S_SV_controlStats="$S_ref_data/control/qc";
$S_SV_controlNA12878_vcf="$S_ref_data/control/qc/SV-CNV_FR05812662.PASS.vcf";
$S_SV_controlSampleID="FR05812662";
$S_control_BW_stem="$S_ref_data/control/cnv";
# lumpy scripts
$S_lumpy_scripts="$S_clinsv_dir/lumpy-sv/scripts";
$S_control_bw_folder="$S_ref_data/control/cnv/bw";
# cnvnator bin
$S_cnvnator_bin="$S_clinsv_dir/cnvnator-multi";
$S_cnvnator_chroms="$S_ref_data/cnvnator_chroms";
$S_root_src="$S_clinsv_dir/root";
# Gemini
$geminiRscr="/scratch/gd7/resources/kccg-gemini";
$geminiBin="/scratch/gd7/resources/gemini/bin";
# for annotate
$S_KDB_SV="$S_ref_data/annotation/MGRB-SV.bed.gz"; # to create see create-KDB-SV.pl in AZ_MA-comparisons.txt
$S_gnomAD_SV="$S_ref_data/annotation/gnomad_v2_sv.sites.reformat.bed.gz"; # to create see create-KDB-SV.pl in AZ_MA-comparisons.txt
$S_HPO_gene2label="$S_ref_data/annotation/Hs-gene-labels.txt";
$S_HPO_gene2hpo="$S_ref_data/annotation/Hs-gene-to-phenotype.txt";
$S_default_track_path="$S_ref_data/tracks";
if ($S_ref_data_v eq "b37"){
$S_lumpy_excludeBed="$S_ref_data/lumpy_exclude.bed";
$S_SV_goldStandard="$S_ref_data/control/qc/GIAB_SV_DEL_gold_noNonCnvGr500_vPB.bed";
$S_SV_control_number_samples=500;
$S_1kG="$S_ref_data/annotation/estd219_1000_Genomes_Consortium_Phase_3_Integrated_SV.2015-07-18.GRCh37.p4.Submitted.gvf.gz";
$S_1kG_igv="$S_default_track_path/DGV_1kG_2015-07-18.igv.bed.gz";
$S_DGV="$S_ref_data/annotation/GRCh37_hg19_variants_2015-07-23";
$S_DGV_igv="$S_default_track_path/GRCh37_hg19_variants_2015-07-23.igv.bed.gz";
$S_genes="$S_ref_data/annotation/Homo_sapiens.GRCh37.75.gff.gz";
$S_SegDup="$S_default_track_path/GRCh37GenomicSuperDup.bed.gz";
$S_Phen="$S_ref_data/annotation/ensemble_GRCh37_2_phen.txt";
$S_gnomAD_SV="$S_ref_data/annotation/gnomad_v2_sv.sites.reformat.bed.gz"; # to create see create-KDB-SV.pl in AZ_MA-comparisons.txt
# ref-data resource
# GATK bundle
#$S_GATK_bundle="/g/data/gd7/resources/gatk-resource-bundle/2.8/b37";
#$refFasta_bwa="$S_GATK_bundle/human_g1k_v37_decoy.fasta";
$refFasta="$S_ref_data/genome/human_g1k_v37_decoy.fasta";
# used in ValidReportPrep.pl script as input
$refChrPf = 'blank';
if ($isHg19)
{
$chrPf='chr';
$refChrPf = 'chr';
}else{
$chrPf='';
}
$wig_line_count=3140518470;
}elsif ($S_ref_data_v eq "b38"){
$S_lumpy_excludeBed="$S_ref_data/exclude.cnvnator_100bp.GRCh38.20170403.bed";
$S_SV_goldStandard="$S_ref_data/control/qc/GIAB_SV_DEL_gold_noNonCnvGr500_vPB_lift_to_b38.bed";
$S_SV_control_number_samples=500;
$S_1kG="$S_ref_data/annotation/1kG_estd219.bed.gz";
$S_1kG_igv="$S_default_track_path/1kG_estd219.igv.bed.gz";
$S_DGV="$S_ref_data/annotation/DGV_GRCh38_hg38_variants_2020-02-25.bed.gz";
$S_DGV_igv="$S_ref_data/tracks/DGV_GRCh38_hg38_variants_2020-02-25.igv.bed.gz";
$S_genes="$S_ref_data/annotation/Homo_sapiens.GRCh38.99.gff.gz";
$S_SegDup="$S_default_track_path/GRCh38GenomicSuperDup.bed.gz";
# this is a file that maps ENSG ID's to OMIM entries (no coordinates), thus can get away with using it in hg38.
# TODO: update this to use the same ENSG ID's as $S_genes
$S_Phen="$S_ref_data/annotation/ensemble_GRCh37_2_phen.txt";
# ref-data resource
# GATK bundle
#$S_GATK_bundle="/g/data/gd7/resources/gatk-resource-bundle/hg38/v0";
#$refFasta_bwa="$S_GATK_bundle/Homo_sapiens_assembly38.fasta";
$refFasta="$S_ref_data/genome/Homo_sapiens_assembly38.fasta";
$chrPf='chr';
# used in ValidReportPrep.pl script as input
$refChrPf = 'chr';
$wig_line_count=3220490784;
}
############### check if user defined run steps are part of above defined pipeline
@allSteps=("bigwig","lumpy","cnvnator","annotate","prioritize","qc","igv");
map {$runSteps{$_}++;} split(",",$runString);
$c=0; map {$allStepsH{$_}=($c++) } @allSteps;
foreach (keys %runSteps){
if (!exists($allStepsH{$_}) and $_ ne "all" ){
print "\n **** error: run step \"$_\" does not exist. Must be one of: ".join(",",(@allSteps))."\n"; printHelp(); exit
}
}
###################### get sample Info
if(! -e $sampleInfoFile){
### check
if($inputAlignDir eq "input"){ $inputAlignDir="$projectDir/$inputAlignDir"; }
@bamsX=glob("$inputAlignDir");
if(@bamsX==0){print {STDERR} "\n **** error: No input bam files in dir: $inputAlignDir or dir does not exist. **** \n"; printHelp(); exit}
print "# Create sample info file from bam files ...\n";
createSampleInfoFromInput();
}
if (! -e $sampleInfoFile){print {STDERR} "\n **** error: The sample info file $sampleInfoFile does not exist **** \n"; printHelp(); exit}
%fastq; readSampleInfo($sampleInfoFile);
print "###### Generate the commands and scripts ######\n\n";
################################################################################
## SV
bigwig() if exists($allStepsH{"bigwig"});
lumpy() if exists($allStepsH{"lumpy"});
cnvnator() if exists($allStepsH{"cnvnator"});
annotate() if exists($allStepsH{"annotate"});
prioritize() if exists($allStepsH{"prioritize"});
qc() if exists($allStepsH{"qc"});
print "###### Run jobs ######\n\n";
################################################################################
undef %cJobsToRun;
undef %aJobsToRun;
$TotalWallTime=0;
undef %TotalWallTimePerStep;
$TotalJobs=0;
getJobsToRun();
executeAllJobs();
igv() if exists($runSteps{"igv"}) or exists($runSteps{"all"}) ;
%sample2batchExt;
########################################
######################################## ClinSV functions
########################################
sub bigwig {
$r_JobType="bigwig";
print "# bigwig\n\n";
$r_head="set -e -x -o pipefail\n\n";
$r_head.="export PATH=$softBin:\$PATH\n";
foreach $cSample (sort keys %fastq){
$r_OutDir="$projectDir/alignments/$cSample/bw";
$rAlnDir="$projectDir/alignments/$cSample";
$r_TmpDir="$r_OutDir/tmp";
$r_JobSubType="createWigs"; #! edit here
$r_JobUnit="$cSample"; #! edit here $cSample or joined
$cJ=setUpJ($r_JobType,$r_JobSubType,$r_JobUnit,$r_OutDir,$r_TmpDir);
$$cJ{rDependencies}=[];
# Using resource specified in resource_available.json
$job_resource = $resource_available->{$r_JobType}->{$r_JobSubType};
print "Using resource:".$job_resource." for job ".$r_JobType.":".$r_JobSubType."\n";
$$cJ{rJobResource}=$job_resource;
open(OUT,">$$cJ{rShStem}.sh");
print OUT "$r_head\n\n";
print OUT "cd $r_OutDir/\n";
#Get max number of CPUs allocated
if ($job_resource =~ /ncpus=(\d+)/)
{
$max_ncpu = $1;
}else{
print STDERR "Unable to extract out max ncpu from $job_resource. Please ensure this is in the form of walltime=hh:mm:ss,ncpus=X,mem=YGB in the -j resource availble json\n";
exit (1);
}
#
if ($max_ncpu <= 1){
print OUT "(( ncpus = `nproc` < 1 ? `nproc` :1))\n";
}else{
#If the num of cpus available is greater than the allocated cpus, use the max allocated. Otherwise use the max cpus available from 'nproc' command. Accounting for 1 cpu used small contigs.
print OUT "(( ncpus = `nproc` < $max_ncpu - 1 ? `nproc` : $max_ncpu - 1 ))\n";
}
foreach $cT (("q0","q20","mq")){ # create bw of s1 for MQ>=0 and MQ>=20
if($cT eq "mq"){
print OUT "\n\nawk '\$2>=100000 {print \$1\":1-\"\$2}' $refFasta.chrom.sizes | xargs -P \${ncpus} -t -i{} perl $S_SV_scriptDir/bam2wigMQ.pl ";
print OUT "-s 1 -r \"{}\" -o $r_TmpDir/$cSample.$cT -f $refFasta -b $rAlnDir/$cSample.bam\n";
print OUT "\n\nawk '\$2<100000 {print \$1\":1-\"\$2}' $refFasta.chrom.sizes | xargs -P 1 -t -i{} perl $S_SV_scriptDir/bam2wigMQ.pl ";
print OUT "-s 1 -r \"{}\" -o $r_TmpDir/$cSample.$cT.small_contigs -f $refFasta -b $rAlnDir/$cSample.bam -a \n";
}else{
print OUT "\n\nawk '\$2>=100000 {print \$1\":1-\"\$2}' $refFasta.chrom.sizes | xargs -P \${ncpus} -t -i{} perl $S_SV_scriptDir/bam2wig.pl ";
print OUT "-s 1 -q $cT -r \"{}\" -o $r_TmpDir/$cSample.$cT -f $refFasta -b $rAlnDir/$cSample.bam\n";
print OUT "\n\nawk '\$2<100000 {print \$1\":1-\"\$2}' $refFasta.chrom.sizes | xargs -P 1 -t -i{} perl $S_SV_scriptDir/bam2wig.pl ";
print OUT "-s 1 -q $cT -r \"{}\" -o $r_TmpDir/$cSample.$cT.small_contigs -f $refFasta -b $rAlnDir/$cSample.bam -a\n";
}
print OUT "cat $r_TmpDir/$cSample.$cT.*.wig > $r_TmpDir/$cSample.$cT.wig\n\n";
print OUT "rm $r_TmpDir/$cSample.$cT.*.wig\n";
}
close(OUT);
}
foreach $cSample (sort keys %fastq){
foreach $cT (("q0","q20","mq")){ # create bw of s1 for MQ>=0 and MQ>=20
$r_OutDir="$projectDir/alignments/$cSample/bw";
$r_TmpDir="$r_OutDir/tmp";
$r_JobSubType="$cT"; #! edit here
$r_JobUnit="$cSample"; #! edit here $cSample or joined
$cJ=setUpJ($r_JobType,$r_JobSubType,$r_JobUnit,$r_OutDir,$r_TmpDir);
$$cJ{rDependencies}=[[$r_JobType,"createWigs",$cSample,"afterok"]]; #! edit here
# Using resource specified in resource_available.json
$job_resource = $resource_available->{$r_JobType}->{$r_JobSubType};
print "Using resource:".$job_resource." for job ".$r_JobType.":".$r_JobSubType."\n";
$$cJ{rJobResource}=$job_resource;
open(OUT,">$$cJ{rShStem}.sh");
print OUT "$r_head\n\n";
print OUT "cd $r_OutDir/\n";
print OUT "wigToBigWig $r_TmpDir/$cSample.$r_JobSubType.wig $refFasta.chrom.sizes $r_OutDir/$cSample.$r_JobSubType.bw\n";
print OUT "## checking the content of bw...\n";
print OUT "bigWigToWig $r_OutDir/$cSample.$r_JobSubType.bw stdout | wc -l > $r_OutDir/$cSample.$r_JobSubType.bw.test\n";
print OUT "wclTest=\$(cat $r_OutDir/$cSample.$r_JobSubType.bw.test)\n";
print OUT "if [ \$wclTest -eq $wig_line_count ]; then echo \"bw size == $wig_line_count OK \"; else echo \"bs size \$wclTest != $wig_line_count, bw potentially truncated\"; exit 111; fi\n\n";
print OUT "rm $r_TmpDir/$cSample.$r_JobSubType.wig\n" if $removeTmpFilesSh;
close(OUT);
}
}
}
sub lumpy {
$r_JobType="lumpy";
$r_head="set -e -x -o pipefail\n\n";
$r_head.=$S_python."export PATH=$softBin:\$PATH\n";
print "\n# $r_JobType\n\n";
################ preprocessing
foreach $cSample (sort keys %fastq){
$r_JobSubType="preproc"; #! edit here
$r_JobUnit="$cSample"; #! edit here: $cSample or joined
$r_OutDir="$projectDir/SVs/$r_JobUnit/lumpy";
$alignOut="$projectDir/alignments/$cSample";
make_path($r_OutDir."/bed") if (! -d $r_OutDir."bed");
$r_TmpDir="$r_OutDir/tmp";
$cJ=setUpJ($r_JobType,$r_JobSubType,$r_JobUnit,$r_OutDir,$r_TmpDir);
$$cJ{rDependencies}=[];
$cMemGB=scalar(keys %{$fastq{$cSample}})*10;
$cCPU=scalar(keys %{$fastq{$cSample}})+1;
$$cJ{rJobResource}="walltime=10:00:00,mem=$cMemGB\gb,ncpus=$cCPU,jobfs=10gb"; #! edit here
# get mean and stdev per sample
open(OUT,">$$cJ{rShStem}.sh");
print OUT "$r_head";
print OUT "perl $S_SV_scriptDir/filterConcordantPairs.pl $alignOut/$cSample.bam 100:100 ".$chrPf."1:1000001-5000000 $refFasta | samtools view -Sb - | samtools sort -m 1G -T $r_TmpDir/$cSample.discordants -o $r_OutDir/$cSample.discordants.bam - &\n";
print OUT "samtools view -T $refFasta -h $alignOut/$cSample.bam | python $S_lumpy_scripts/extractSplitReads_BwaMem -i stdin | awk '\$3 != \"hs37d5\" && \$3 != \"NC_007605\"' | samtools view -Sb - | samtools sort -m 1G -T $r_TmpDir/$cSample.splitters -o $r_OutDir/$cSample.splitters.bam - &\n";
print OUT "wait\n";
print OUT "perl $S_SV_scriptDir/PE-SR-bam2bed.pl splitters $r_OutDir $cSample $refFasta $r_OutDir/$cSample.splitters.bam | head & \n";
print OUT "perl $S_SV_scriptDir/PE-SR-bam2bed.pl discordants $r_OutDir $cSample $refFasta $r_OutDir/$cSample.discordants.bam | head & \n";
print OUT "wait\n";
print OUT "samtools index $r_OutDir/$cSample.discordants.f.bam &\n";
print OUT "samtools sort -m 2G -T $r_TmpDir/$cSample.splitters -o $r_OutDir/$cSample.splitters.fs.bam $r_OutDir/$cSample.splitters.f.bam \n";
print OUT "mv $r_OutDir/$cSample.splitters.fs.bam $r_OutDir/$cSample.splitters.f.bam \n";
print OUT "samtools index $r_OutDir/$cSample.splitters.f.bam &\n";
print OUT "wait\n";
print OUT "[ -d \"$r_TmpDir\" ] && rm -r $r_TmpDir\n\n" if $removeTmpFilesSh;
close(OUT);
}
################ lumpy main
@SampleOrder=sort keys %fastq;
$lumpOrSpeedPreProc="OpreProc";#$lumpOrSpeedPreProc="speed";
$LP_mw=3; # more coverage results pairs overlapping by chance, changing the minimum weight doesn't change the fact. The extreme case would be a constant coverage greater than 1x. The clustering Program should pack everying into one breakpoint!?
# so this is a clear limitation of lumpy. What could downsample, but that would be a pitty for the larger events which would loose evidence.
# if one realy wants to get the small PEs one would have to use a program like CLEVER, I guess
# For now I will just adjust the SD for skewness and coverage together and keep the weight the same for all smaples
# with depth integration I aim at either: min 1PE and 1SR, or min 3PEs or min 2SR
# -msw $LP_msw, seems useless because both msw and mw needs to be met and we want variants just present in only one sample to be called
$r_OutDir="$projectDir/SVs/$nameStemJoinF/lumpy";
for($i=0; $i<=$#SampleOrder; $i+=$lumpyBatchSize){
$LP_z=3;$LP_srw=2;$LP_MQ=20;
$cSSt=($i+1);$cSEn=( ($i+$lumpyBatchSize) <= scalar(@SampleOrder) )? ($i+$lumpyBatchSize):scalar(@SampleOrder);
$fileExt=(@SampleOrder>$lumpyBatchSize)? "_".$cSSt."-".$cSEn:"";
$r_JobUnit="joined$fileExt"; #! edit here: $cSample or joined
for $cSpl ($i..($cSEn-1)){ $sample2batchExt{$SampleOrder[$cSpl]}=$fileExt; }
####### lumpy caller
$r_JobSubType="caller"; #! edit here
$cJ=setUpJ($r_JobType,$r_JobSubType,$r_JobUnit,$r_OutDir);
$$cJ{rDependencies}=[["lumpy","preproc","all","afterok"]]; #! edit here
# Using resource specified in resource_available.json
$job_resource = $resource_available->{$r_JobType}->{$r_JobSubType};
print "Using resource:".$job_resource." for job ".$r_JobType.":".$r_JobSubType."\n";
$$cJ{rJobResource}=$job_resource;
# $$cJ{rQueueType}="express";
open(OUT,">$$cJ{rShStem}.sh");
print OUT "$r_head\ndeclare -A meanArr; declare -A stdevArr; declare -A readLArr; \n";
for $ii ($cSSt..$cSEn){
$cSample=$SampleOrder[($ii-1)];
$r_PreProc="$projectDir/SVs/$cSample/lumpy";
foreach $cReadGroupID (keys %{$fastq{$cSample}}){
$outStemSC=$cReadGroupID; $outStemSC=~ s/\-/_/g;
print OUT "readLArr[$outStemSC]=\$(samtools view $projectDir/alignments/$cSample/$cSample.bam ".$chrPf."1:1000000-1100000 | cut -f 10 | awk '{ print length}' | sort -rn | awk '(NR==1){print}' )\n";
print OUT "read -r mean stdev <<< \$( samtools view -r $cReadGroupID $projectDir/alignments/$cSample/$cSample.bam | python3 $S_SV_scriptDir/pairend_distro-a1.py -r \${readLArr[$outStemSC]} -X 2 -N 100000 -o $r_PreProc/$cReadGroupID.pe.histo | cut -d \":\" -f 2 )\n";
print OUT "meanArr[$outStemSC]=\$mean;stdevArr[$outStemSC]=\$stdev;\n";
}
}
print OUT "cd $r_OutDir \n";
print OUT "lumpy -mw $LP_mw -tt 0 -x $S_lumpy_excludeBed \\\n";
for $ii ($cSSt..$cSEn){
$cSample=$SampleOrder[($ii-1)];
$PESR_dir=($lumpOrSpeedPreProc eq "OpreProc")? "SVs/$cSample/lumpy":"alignments/$cSample";
$r_PreProc="$projectDir/SVs/$cSample/lumpy";
foreach $cReadGroupID (keys %{$fastq{$cSample}}){
$outStemSC=$cReadGroupID; $outStemSC=~ s/\-/_/g;
print OUT "-pe id:$cSample,bam_file:$projectDir/$PESR_dir/$cSample.discordants.bam,read_group:$cReadGroupID,histo_file:$r_PreProc/$cReadGroupID.pe.histo,mean:\${meanArr[$outStemSC]},stdev:\${stdevArr[$outStemSC]},read_length:\${readLArr[$outStemSC]},min_non_overlap:\$(expr \${readLArr[$outStemSC]} - 30),discordant_z:$LP_z,back_distance:10,weight:1,min_mapping_threshold:$LP_MQ \\\n";
}
print OUT "-sr id:$cSample,bam_file:$projectDir/$PESR_dir/$cSample.splitters.f.bam,back_distance:10,weight:$LP_srw,min_mapping_threshold:$LP_MQ \\\n";
}
print OUT " > $r_OutDir/$nameStemJoint.MQ$LP_MQ.$lumpOrSpeedPreProc$fileExt.vcf\n";
close(OUT);
}
for($i=0; $i<=$#SampleOrder; $i+=$lumpyBatchSize){
$LP_z=3;$LP_srw=2;$LP_MQ=20;
$cSSt=($i+1);$cSEn=( ($i+$lumpyBatchSize) <= scalar(@SampleOrder) )? ($i+$lumpyBatchSize):scalar(@SampleOrder);
$fileExt=(@SampleOrder>$lumpyBatchSize)? "_".$cSSt."-".$cSEn:"";
$r_JobUnit="joined$fileExt"; #! edit here: $cSample or joined
####### run the depth addition
$r_JobSubType="depth"; #! edit here
$r_TmpDir="$r_OutDir/tmp";
$cJ=setUpJ($r_JobType,$r_JobSubType,$r_JobUnit,$r_OutDir,$r_TmpDir);
$$cJ{rDependencies}=[["lumpy","caller",$r_JobUnit,"afterok"],["bigwig","q0","all","afterok"],["bigwig","q20","all","afterok"],["bigwig","mq","all","afterok"]]; #! edit here
# Using resource specified in resource_available.json
$job_resource = $resource_available->{$r_JobType}->{$r_JobSubType};
print "Using resource:".$job_resource." for job ".$r_JobType.":".$r_JobSubType."\n";
$$cJ{rJobResource}=$job_resource;
#Get max number of CPUs allocated
if ($job_resource =~ /ncpus=(\d+)/)
{
$max_ncpu = $1;
}else{
print STDERR "Unable to extract out max ncpu from $job_resource. Please ensure this is in the form of walltime=hh:mm:ss,ncpus=X,mem=YGB in the -j resource availble json\n";
exit (1);
}
# $$cJ{rQueueType}="express";
open(OUT,">$$cJ{rShStem}.sh");
$r_head.="export PERL5LIB=$S_clinsv_dir/perlib:\$PERL5LIB\n";
print OUT "$r_head\n";
#If the num of cpus available is greater than the allocated cpus, use the max allocated. Otherwise use the max cpus available from 'nproc' command. Accounting for 1 cpu used small contigs.
print OUT "(( ncpus = `nproc` < $max_ncpu ? `nproc` : $max_ncpu ))\n";
# parallel version with local copy of bw files
print OUT "perl -e 'while(<>){ if(/^#/){\$h.=\$_; next;} \@_=split(\"\\t\",\$_); \$c=\$_[0]; if(!exists(\$f{\$c})){ open(\$f{\$c},\">$r_TmpDir/in.\$c.vcf\"); print {\$f{\$c}} \$h; } \n";
print OUT "print {\$f{\$c}} \$_; } foreach (values %f){close} ' $r_OutDir/$nameStemJoint.MQ$LP_MQ.$lumpOrSpeedPreProc$fileExt.vcf \n";
print OUT "ls $r_TmpDir/in.*.vcf | xargs -P \${ncpus} -t -i{} perl $S_SV_scriptDir/add-depth-to-PE-SR-calls.pl {} $refFasta ".$refChrPf." $projectDir $S_SV_controlSRPE $S_SV_control_number_samples $S_control_bw_folder \n";
print OUT "(grep \"#\" $r_TmpDir/in.".$chrPf."1.out; cat $r_TmpDir/in.*.out | grep -v \"#\" | sort -V -k1,1 -k2,2n ) > $r_OutDir/$nameStemJoint.MQ$LP_MQ.$lumpOrSpeedPreProc$fileExt.f1.vcf\n";
# not parrallel
# print OUT "perl $S_SV_scriptDir/add-depth-to-PE-SR-calls.pl $r_OutDir/$nameStemJoint.MQ$LP_MQ.$lumpOrSpeedPreProc$fileExt.vcf $refFasta $projectDir $r_TmpDir\n";
print OUT "perl $S_SV_scriptDir/split_lumpy_vcf_by_sample.pl --infoFields SVTYPE,LEN --gtsFields SR,PE,DRF,IDD,CNRD,CNP --PASS --input $r_OutDir/$nameStemJoint.MQ$LP_MQ.$lumpOrSpeedPreProc$fileExt.f1.vcf --outStem $r_OutDir/$nameStemJoint.MQ$LP_MQ.$lumpOrSpeedPreProc$fileExt.f1\n";
print OUT "inVCFS=$r_OutDir/$nameStemJoint.MQ$LP_MQ.$lumpOrSpeedPreProc$fileExt.f1.vcf\n";
print OUT "sort_bgzip \$inVCFS; tabix -f -p vcf \$inVCFS.gz\n\n";
print OUT "[ -d \"$r_TmpDir\" ] && rm -r $r_TmpDir\n\n";
close(OUT);
}
}
sub cnvnator {
$r_JobType="cnvnator";
print "\n# $r_JobType\n\n";
### write cnvnator commands sampleInfoFile
$r_head="";
$r_head.="export PATH=$softBin:\$PATH\n";
$r_head.=$S_python;
# $r_head.= "source $S_root_src/bin/thisroot.sh\n";
$r_head.="set -e -x -o pipefail\n\n";
foreach $cSample (sort keys %fastq){
$r_JobSubType="caller"; #! edit here
$r_JobUnit="$cSample"; #! edit here $cSample or joined
$r_OutDir="$projectDir/SVs/$cSample/cnvnator";
$r_TmpDir="$r_OutDir/tmp";
$cJ=setUpJ($r_JobType,$r_JobSubType,$r_JobUnit,$r_OutDir,$r_TmpDir);
$$cJ{rDependencies}=[["lumpy","preproc",$cSample,"afterok"],["bigwig","q0",$cSample,"afterok"],["bigwig","q20",$cSample,"afterok"],["bigwig","mq",$cSample,"afterok"]]; #! edit here
# Using resource specified in resource_available.json
$job_resource = $resource_available->{$r_JobType}->{$r_JobSubType};
print "Using resource:".$job_resource." for job ".$r_JobType.":".$r_JobSubType."\n";
$$cJ{rJobResource}=$job_resource;
open(OUT,">$$cJ{rShStem}.sh");
print OUT "$r_head\n";
print OUT "cd $r_OutDir/\n";
print OUT "python $S_cnvnator_bin/cnvnator_wrapper.py --cnvnator $S_cnvnator_bin/cnvnator-multi ";
print OUT "-T $r_TmpDir -t 14 -w 100 -b $projectDir/alignments/$cSample/$cSample.bam -o $r_OutDir/cnvnator.$cSample ";
print OUT "-c $S_cnvnator_chroms -g $S_ref_data_v --exclude '_,HLA,EBV' \n"; #Excluding superflous contigs
print OUT "perl $S_SV_scriptDir/filterCNVNator.pl $r_OutDir/cnvnator.$cSample.txt $cSample $refFasta ".$refChrPf." $projectDir $S_SV_controlSRPE $S_SV_control_number_samples $S_control_bw_folder\n\n";
print OUT "[ -d \"$r_TmpDir\" ] && rm -r $r_TmpDir\n\n" if $removeTmpFilesSh;
close(OUT);
}
}
sub annotate {
$r_JobType="annotate";
print "\n# $r_JobType\n\n";
$r_JobSubType="main"; #! edit here
@SampleOrder=sort keys %fastq;
for($i=0; $i<=$#SampleOrder; $i+=$lumpyBatchSize){
$cSSt=($i+1);$cSEn=( ($i+$lumpyBatchSize) <= scalar(@SampleOrder) )? ($i+$lumpyBatchSize):scalar(@SampleOrder);
$fileExt=(@SampleOrder>$lumpyBatchSize)? "_".$cSSt."-".$cSEn:"";
$r_JobUnit="joined$fileExt"; #! edit here: $cSample or joined
@cSampleOrder=@SampleOrder[($cSSt-1)..($cSEn-1)];
$r_OutDir="$projectDir/SVs/$nameStemJoinF";
$r_TmpDir="$r_OutDir/tmp";
$cJ=setUpJ($r_JobType,$r_JobSubType,$r_JobUnit,$r_OutDir,$r_TmpDir);
$$cJ{rDependencies}=[["cnvnator","caller","all","afterok"],["lumpy","depth","all","afterok"]]; #! edit here
# if SNVs are to be called, wait for those to do the LOH annotation
push @{$$cJ{rDependencies}}, ["vqsr","VariantRecalibrator","joined","afterok"] if exists($allStepsH{"vqsr"});
push @{$$cJ{rDependencies}}, ["freebayes","caller","all","afterok"] if exists($allStepsH{"freebayes"}) ;
# Using resource specified in resource_available.json
$job_resource = $resource_available->{$r_JobType}->{$r_JobSubType};
print "Using resource:".$job_resource." for job ".$r_JobType.":".$r_JobSubType."\n";
$$cJ{rJobResource}=$job_resource;
# $$cJ{rQueueType}="normal";
undef @extensions;
### write annotate commands sampleInfoFile
$r_head="set -e -x -o pipefail\n\n";
$r_head.="export PATH=$softBin:\$PATH\n";
$r_head.="export PERL5LIB=$S_clinsv_dir/perlib:\$PERL5LIB\n";
$r_head.="cd $r_OutDir\n\n";
open(OUT,">$$cJ{rShStem}.sh");
print OUT "$r_head\n";
##### merge Lumpy and CNVNator variants
print OUT "perl $S_SV_scriptDir/merge-lumpy-CNVNator.pl ".
"$projectDir/SVs/XX_SAMPLEID_XX/cnvnator/cnvnator.XX_SAMPLEID_XX.f1.bed ".join(",",@cSampleOrder)." ".
"$r_OutDir/lumpy/$nameStemJoint.MQ20.OpreProc$fileExt.f1.vcf $r_TmpDir/SV-CNV$fileExt\n\n";
print OUT "inVCFS=$r_TmpDir/SV-CNV$fileExt\n";
print OUT "sort_bgzip \$inVCFS.vcf; tabix -f -p vcf \$inVCFS.vcf.gz\n\n";
##### annotation DGV
print OUT "perl $S_SV_scriptDir/annotate-DGV.pl -dgv $S_DGV -toAnnot $r_TmpDir/SV-CNV$fileExt.vcf.gz --ref $refFasta\n\n";
push @extensions, 'DGV'; $cExt=join(".",@extensions);
##### annotation GC
print OUT "perl $S_SV_scriptDir/annotate-GC.pl $r_TmpDir/SV-CNV$fileExt.$cExt.vcf $refFasta > $r_TmpDir/SV-CNV$fileExt.$cExt.GC.vcf\n\n";
push @extensions, 'GC'; $cExt=join(".",@extensions);
##### annotation LOH
if ( exists($allStepsH{"vqsr"}) or ( -e "$projectDir/SNVs/$nameStemJoinF/$nameStemJoint.vqsr.vcf.gz" ) ){
print OUT "cp $projectDir/SNVs/$nameStemJoinF/$nameStemJoint.vqsr.vcf.gz \$PBS_JOBFS/ \n";
print OUT "cp $projectDir/SNVs/$nameStemJoinF/$nameStemJoint.vqsr.vcf.gz.tbi \$PBS_JOBFS/ \n";
print OUT "perl $S_SV_scriptDir/annotate-LOH.pl $r_TmpDir/SV-CNV$fileExt.$cExt.vcf \\\n";
print OUT "\$PBS_JOBFS/$nameStemJoint.vqsr.vcf.gz > $r_TmpDir/SV-CNV$fileExt.$cExt.LOH.vcf \n\n";
push @extensions, 'LOH'; $cExt=join(".",@extensions);
}elsif( exists($allStepsH{"freebayes"}) or ( -e "$projectDir/SNVs/$nameStemJoinF/$nameStemJoint.fb.vcf.gz" ) ){
print OUT "cp $projectDir/SNVs/$nameStemJoinF/$nameStemJoint.fb.vcf.gz \$PBS_JOBFS/ \n";
print OUT "cp $projectDir/SNVs/$nameStemJoinF/$nameStemJoint.fb.vcf.gz.tbi \$PBS_JOBFS/ \n";
print OUT "perl $S_SV_scriptDir/annotate-LOH.pl $r_TmpDir/SV-CNV$fileExt.$cExt.vcf \\\n";
print OUT "\$PBS_JOBFS/$nameStemJoint.fb.vcf.gz > $r_TmpDir/SV-CNV$fileExt.$cExt.LOH.vcf \n\n";
push @extensions, 'LOH'; $cExt=join(".",@extensions);
}
##### annotation segDups: Segmental duplication annotation:: For best match: % query coverage | % target (seg-dup) coverage | identity | For all matches: count | merged % coverage of querry length
print OUT "perl $S_SV_scriptDir/annotate-segDups.pl $S_SegDup ";
print OUT "$r_TmpDir/SV-CNV$fileExt.$cExt.vcf > $r_TmpDir/SV-CNV$fileExt.$cExt.SEGD.vcf\n\n";
push @extensions, 'SEGD'; $cExt=join(".",@extensions);
##### annotation KCCG SV database (Ovl. with calls form control samples): varient frequency (%ovl more tolerant for CNVnator), method,
print OUT "perl $S_SV_scriptDir/annotate-KDB.pl $S_KDB_SV $S_SV_control_number_samples $r_TmpDir/SV-CNV$fileExt.$cExt.vcf > $r_TmpDir/SV-CNV$fileExt.$cExt.KDB.vcf\n\n";
push @extensions, 'KDB'; $cExt=join(".",@extensions);
##### annotation gnomed VAF
if ($S_ref_data_v eq "b37"){
print OUT "perl $S_SV_scriptDir/annotate-gnomAD.pl $S_gnomAD_SV $r_TmpDir/SV-CNV$fileExt.$cExt.vcf > $r_TmpDir/SV-CNV$fileExt.$cExt.gnomAD.vcf\n\n";
push @extensions, 'gnomAD'; $cExt=join(".",@extensions);
}
##### annotation KCCG SV database (Ovl. with calls form control samples): varient frequency (%ovl more tolerant for CNVnator), method,
print OUT "perl $S_SV_scriptDir/annotate-1kG.pl -dgva $S_1kG -toAnnot $r_TmpDir/SV-CNV$fileExt.$cExt.vcf \n\n";
push @extensions, '1kG'; $cExt=join(".",@extensions);
##### annotate with my ENSEMBL gene annotation script
print OUT "perl $S_SV_scriptDir/annotate-ENSEMBL.pl $S_genes \\\n";
print OUT "$r_TmpDir/SV-CNV$fileExt.$cExt.vcf $S_HPO_gene2label $S_HPO_gene2hpo > $r_TmpDir/SV-CNV$fileExt.$cExt.ENS.vcf \n\n";
push @extensions, 'ENS'; $cExt=join(".",@extensions);
##### add missing quality stags/flags for unmerged CNVnator rows,
# since it is compute intensive only do it for events overlapping genes and not present in KDB
print OUT "perl $S_SV_scriptDir/qualStats_CNVNator.pl ".join(",",(@SampleOrder[($cSSt-1)..($cSEn-1)]))." $S_control_BW_stem $refFasta $projectDir \\\n";
print OUT " $r_TmpDir/SV-CNV$fileExt.$cExt.vcf > $r_TmpDir/SV-CNV$fileExt.$cExt.qsCNV.vcf\n\n";
push @extensions, 'qsCNV'; $cExt=join(".",@extensions);
print OUT "ln -fs ./SV-CNV$fileExt.$cExt.vcf $r_TmpDir/SV-CNV$fileExt.annot.vcf\n\n";
#print OUT "[ -d \"$r_TmpDir\" ] && rm -r $r_TmpDir\n\n" if $removeTmpFilesSh and @SampleOrder<$lumpyBatchSize;
close(OUT);
}
}
sub prioritize {
$r_JobType="prioritize";
print "\n# $r_JobType\n\n";
$r_JobSubType="main"; #! edit here
@SampleOrder=sort keys %fastq;
for($i=0; $i<=$#SampleOrder; $i+=$lumpyBatchSize){
$cSSt=($i+1);$cSEn=( ($i+$lumpyBatchSize) <= scalar(@SampleOrder) )? ($i+$lumpyBatchSize):scalar(@SampleOrder);
$fileExt=(@SampleOrder>$lumpyBatchSize)? "_".$cSSt."-".$cSEn:"";
$r_JobUnit="joined$fileExt"; #! edit here: $cSample or joined
@cSampleOrder=@SampleOrder[($cSSt-1)..($cSEn-1)];
$r_OutDir="$projectDir/SVs/$nameStemJoinF";
$r_TmpDir="$r_OutDir/tmp";
$cJ=setUpJ($r_JobType,$r_JobSubType,$r_JobUnit,$r_OutDir,$r_TmpDir);
$$cJ{rDependencies}=[["annotate","main","all","afterok"]]; #! edit here
# Using resource specified in resource_available.json
$job_resource = $resource_available->{$r_JobType}->{$r_JobSubType};
print "Using resource:".$job_resource." for job ".$r_JobType.":".$r_JobSubType."\n";
$$cJ{rJobResource}=$job_resource;
# $$cJ{rQueueType}="normal";
### write annotsv commands sampleInfoFile
$r_head="set -e -x -o pipefail\n\n";
$r_head.="export PATH=$softBin:\$PATH\n";
$r_head.="export PERL5LIB=$S_clinsv_dir/perlib:\$PERL5LIB\n";
$r_head.="cd $r_OutDir\n\n";
open(OUT,">$$cJ{rShStem}.sh");
print OUT "$r_head\n";
make_path($projectDir."/results") if (! -d $projectDir."/results");
$projectDir_adj= length($projectDir_host)<1 ? $projectDir:$projectDir_host;
##### prioritize SV
print OUT "perl $S_SV_scriptDir/prioritize-SVs.pl $r_TmpDir/SV-CNV$fileExt.annot $projectDir $S_Phen $projectDir_adj/igv $refFasta \n\n";
$xlx_stem=($lumpyBatchSize==1 or scalar(@SampleOrder)==1)? $cSampleOrder[0]:"SV-CNV$fileExt";
print OUT "cp $r_TmpDir/SV-CNV$fileExt.annot.prioritized.RARE_PASS_GENE.light.xlsx $projectDir/results/$xlx_stem.RARE_PASS_GENE.light.xlsx \n\n";
print OUT "cp $r_TmpDir/SV-CNV$fileExt.annot.prioritized.RARE_PASS_GENE.xlsx $projectDir/results/$xlx_stem.RARE_PASS_GENE.xlsx \n\n";
print OUT "cp $S_ref_data/result_description.docx $projectDir/results/ \n\n";
print OUT "cp $r_TmpDir/SV-CNV$fileExt.annot.prioritized.vcf $r_OutDir/SV-CNV$fileExt.vcf\n\n";
print OUT "cp $r_TmpDir/SV-CNV$fileExt.annot.prioritized.txt $r_OutDir/SV-CNV$fileExt.txt\n\n";
print OUT "cp $r_TmpDir/SV-CNV$fileExt.annot.prioritized.RARE_PASS_GENE.xlsx $r_OutDir/SV-CNV$fileExt.RARE_PASS_GENE.xlsx\n\n";
print OUT "cp $r_TmpDir/SV-CNV$fileExt.annot.prioritized.RARE_PASS_GENE.light.xlsx $r_OutDir/SV-CNV$fileExt.RARE_PASS_GENE.light.xlsx\n\n";
print OUT "cp $r_TmpDir/SV-CNV$fileExt.annot.prioritized.RARE_PASS_GENE.txt $r_OutDir/SV-CNV$fileExt.RARE_PASS_GENE.txt\n\n";
print OUT "cp $r_TmpDir/SV-CNV$fileExt.annot.prioritized.RARE_PASS_GENE.vcf $r_OutDir/SV-CNV$fileExt.RARE_PASS_GENE.vcf\n\n";
print OUT "cp $r_TmpDir/SV-CNV$fileExt.annot.prioritized.PASS.vcf $r_OutDir/SV-CNV$fileExt.PASS.vcf\n\n";
print OUT "inVCFS=$r_OutDir/SV-CNV$fileExt\n";
print OUT "sort_bgzip \$inVCFS.vcf; tabix -f -p vcf \$inVCFS.vcf.gz\n\n";
print OUT "perl $S_SV_scriptDir/SV_vcf_2_bed.pl $r_OutDir/SV-CNV$fileExt $r_OutDir/SV-CNV$fileExt.vcf \n\n";
# print OUT "[ -d \"$r_TmpDir\" ] && rm -r $r_TmpDir\n\n" if $removeTmpFilesSh and @SampleOrder<$lumpyBatchSize;
close(OUT);
}
}
sub qc {
$r_JobType="qc";
print "\n# $r_JobType\n\n";
$r_JobSubType="main"; #! edit here
@SampleOrder=sort keys %fastq;
for($i=0; $i<=$#SampleOrder; $i+=$lumpyBatchSize){
$cSSt=($i+1);$cSEn=( ($i+$lumpyBatchSize) <= scalar(@SampleOrder) )? ($i+$lumpyBatchSize):scalar(@SampleOrder);
$fileExt=(@SampleOrder>$lumpyBatchSize)? "_".$cSSt."-".$cSEn:"";
$r_JobUnit="joined$fileExt"; #! edit here: $cSample or joined
@cSampleOrder=@SampleOrder[($cSSt-1)..($cSEn-1)];
$r_OutDir="$projectDir/SVs/qc";
$r_SVDir="$projectDir/SVs/$nameStemJoinF";
$r_TmpDir="$r_OutDir/tmp";
$cJ=setUpJ($r_JobType,$r_JobSubType,$r_JobUnit,$r_OutDir,$r_TmpDir);
$$cJ{rDependencies}=[["prioritize","main","all","afterok"]]; #! edit here
# Using resource specified in resource_available.json
$job_resource = $resource_available->{$r_JobType}->{$r_JobSubType};
print "Using resource:".$job_resource." for job ".$r_JobType.":".$r_JobSubType."\n";
$$cJ{rJobResource}=$job_resource;
# $$cJ{rQueueType}="normal";
### write qc commands sampleInfoFile
$r_head="set -e -x -o pipefail\n\n";
$r_head.="export PATH=$softBin:\$PATH\n";
$r_head.="export PERL5LIB=$S_clinsv_dir/perlib:\$PERL5LIB\n";
$r_head.="cd $r_OutDir\n\n";
open(OUT,">$$cJ{rShStem}.sh");
print OUT "$r_head\n";
########## eval report
print OUT "\n## evaluation report\n\n";
foreach $cSample (@cSampleOrder){
print OUT "bcftools view -s $cSample $r_SVDir/SV-CNV$fileExt.vcf.gz | bcftools view -e ' FMT/FT!=\"PASS\" & FMT/FT!=\"HIGH\" ' > $r_SVDir/SV-CNV$fileExt.$cSample.PASS.vcf\n";
make_path($projectDir."/SVs/qc/tmpQC") if (! -d $r_SVDir."/SVs/qc/tmpQC");
###### prepare data
print OUT "perl $S_SV_scriptDir/validReportPrep.pl $projectDir $refFasta ".$refChrPf." \"$fileExt\" $cSample $nameStemJoinF\n\n";
if($eval){
foreach $cType (("highCNV","passCNV","highSV","passSV")){
###### reproducibility analysis with control samples
make_path($projectDir."/SVs/qc/reproducibility/$cSample/$cType\_rS_rC") if (! -d $r_OutDir."/SVs/qc/reproducibility/$cSample/$cType\_rS_rC");
print OUT "cd $projectDir/SVs/qc/reproducibility/$cSample/$cType\_rS_rC \n";
print OUT "perl $S_SV_scriptDir/conc_sv.pl -$cType -useVType -cnvPercentOvl 80 -a $r_SVDir/SV-CNV$fileExt.$cSample.PASS.vcf -b $S_SV_controlNA12878_vcf > resulTable.tab \n\n";
}
foreach $cType (("highCNV","passCNV","highSV","passSV")){
###### reproducibility analysis with control samples
make_path($projectDir."/SVs/qc/reproducibility/$cSample/$cType\_rC_rS") if (! -d $r_OutDir."/SVs/qc/reproducibility/$cSample/$cType\_rC_rS");
print OUT "cd $projectDir/SVs/qc/reproducibility/$cSample/$cType\_rC_rS \n";
print OUT "perl $S_SV_scriptDir/conc_sv.pl -$cType -useVType -cnvPercentOvl 80 -b $r_SVDir/SV-CNV$fileExt.$cSample.PASS.vcf -a $S_SV_controlNA12878_vcf > resulTable.tab \n\n";
}
###### sensitivity analysis with gold standard
make_path($projectDir."/SVs/qc/sensitivity/$cSample") if (! -d $r_SVDir."/SVs/qc/sensitivity/$cSample");
print OUT "cd $projectDir/SVs/qc/sensitivity/$cSample \n";
print OUT "perl $S_SV_scriptDir/conc_sv.pl -useVType -cnvPercentOvl 80 -a $S_SV_goldStandard -b $r_SVDir/SV-CNV$fileExt.$cSample.PASS.vcf > resulTable.tab \n\n";
}
###### plot and make a pdf
print OUT "export PARAM_PROJECT_PATH=$projectDir \n";
print OUT "export PARAM_CONTROL_SAMPLE=$S_SV_controlSampleID \n";
print OUT "export PARAM_SAMPLE=$cSample \n";
print OUT "export PARAM_ControlStats_PATH=$S_SV_controlStats \n\n";
print OUT "export PARAM_EVAL=$eval \n\n";
print OUT "mkdir -p $projectDir/SVs/qc/tmpQC/tmp/$cSample/figure \n";
print OUT "cp $S_SV_controlStats/*.png $projectDir/SVs/qc/tmpQC/tmp/$cSample/figure/\n";
print OUT "cd $projectDir/SVs/qc/tmpQC/tmp/$cSample \n";
print OUT "export R_ROOT_DIR=\$(dirname \$(dirname \$(readlink -f \$(which R)))) \n";
print OUT "export RHOME=\${R_ROOT_DIR} \n";
print OUT "R -e \"library(knitr); knit('$S_SV_scriptDir/validReport.Rnw', output = '$projectDir/SVs/qc/tmpQC/$cSample.QC_report.tex')\" \n";
print OUT "pdflatex -output-directory $projectDir/SVs/qc/tmpQC -interaction nonstopmode $projectDir/SVs/qc/tmpQC/$cSample.QC_report.tex || true\n";
print OUT "cp $projectDir/SVs/qc/tmpQC/$cSample.QC_report.pdf $projectDir/SVs/qc/ \n\n";
print OUT "rm -r $projectDir/SVs/qc/tmpQC/tmp/$cSample \n";
print OUT "cp $projectDir/SVs/qc/$cSample*.pdf $projectDir/results/ \n\n";
}
# print OUT "[ -d \"$r_TmpDir\" ] && rm -r $r_TmpDir\n\n" if $removeTmpFilesSh and @SampleOrder<$lumpyBatchSize;
close(OUT);
}
}
sub igv {
print "# writing igv session files...\n\n";
$S_igv_dir="$projectDir/igv";
if (length($projectDir_host)<1){
$session_target_path="$projectDir";
}else{
$session_target_path="$projectDir_host";
}
if (length($S_ref_data_host)<1){
$S_default_track_path_xml="$S_ref_data/tracks";
}else{
$S_default_track_path_xml="$S_ref_data_host/tracks";
}
if ($igvweb) {
$S_default_track_path_xml="https://nci.space/clinsv/clinsv_$S_ref_data_v/refdata/tracks";
}
make_path($S_igv_dir) if (! -d $S_igv_dir);
# create the IGV session
foreach $cSample (sort keys %fastq){
undef %IGVS; undef %IGVOpt;
##### coverage tracks #### panel 1
# NA12878
$IGVS{1}{1}{path}="$S_default_track_path_xml/FR05812622.q0.bw";
$IGVS{1}{1}{name}="Coverage NA12878";
# Sample coverage
$IGVS{1}{2}{path}="$session_target_path/alignments/$cSample/bw/$cSample.q0.bw";
$IGVS{1}{2}{color}="0,51,153";
$IGVS{1}{2}{name}="Coverage $cSample";
$IGVS{1}{3}{path}="$session_target_path/alignments/$cSample/bw/$cSample.q20.bw";
$IGVS{1}{3}{color}="0,102,153";
$IGVS{1}{3}{name}="Coverage >= MQ20 $cSample";
$IGVS{1}{4}{path}="$session_target_path/alignments/$cSample/bw/$cSample.mq.bw";
$IGVS{1}{4}{height}=30;
$IGVS{1}{4}{name}="Mapping qual. $cSample";
$IGVS{1}{5}{path}="$S_default_track_path_xml/popCovStdev.bw";
$IGVS{1}{5}{color}="0,102,102";
$IGVS{1}{5}{height}=30;
$IGVS{1}{5}{name}="Pop. cov. stdev";
$IGVS{1}{5}{DataRange}{maximum}=0.5;
$IGVS{1}{5}{autoScale}="false";
# Sample segdups
#$IGVS{1}{6}{path}="$S_default_track_path_xml/GRCh37GenomicSuperDup.bed.gz";
$IGVS{1}{6}{path} = $S_SegDup;
$IGVS{1}{6}{path} =~ s/$S_default_track_path/$S_default_track_path_xml/;
$IGVS{1}{6}{name}="Segmental Duplications";
##### SV track #### panel 2
if ( -e "$projectDir/SVs/$nameStemJoinF/SV-CNV".$sample2batchExt{$cSample}.".$cSample.igv.bed.gz"){
$IGVS{2}{1}{path}="$session_target_path/SVs/$nameStemJoinF/SV-CNV".$sample2batchExt{$cSample}.".$cSample.igv.bed.gz" ;
$IGVS{2}{1}{name}="ClinSV $cSample";
}else{
print "lumpy variants not present: $projectDir/SVs/$nameStemJoinF/SV-CNV".$sample2batchExt{$cSample}.".$cSample.igv.bed.gz\n";
}
##### discordant reads #### panel 3
if(-e "$projectDir/SVs/$cSample/lumpy/bed/$cSample.discordants.bed.gz"){ $IGVS{3}{1}{path}="$session_target_path/SVs/$cSample/lumpy/bed/$cSample.discordants.bed.gz"; }