forked from gatk-workflows/gatk4-germline-snps-indels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joint-discovery-gatk4.wdl
1004 lines (888 loc) · 33.1 KB
/
joint-discovery-gatk4.wdl
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
## Copyright Broad Institute, 2018
##
## This WDL implements the joint discovery and VQSR filtering portion of the GATK
## Best Practices (June 2016) for germline SNP and Indel discovery in human
## whole-genome sequencing (WGS) and exome sequencing data.
##
## Requirements/expectations :
## - One or more GVCFs produced by HaplotypeCaller in GVCF mode
## - Bare minimum 1 WGS sample or 30 Exome samples. Gene panels are not supported.
##
## Outputs :
## - A VCF file and its index, filtered using variant quality score recalibration
## (VQSR) with genotypes for all samples present in the input VCF. All sites that
## are present in the input VCF are retained; filtered sites are annotated as such
## in the FILTER field.
##
## Note about VQSR wiring :
## The SNP and INDEL models are built in parallel, but then the corresponding
## recalibrations are applied in series. Because the INDEL model is generally ready
## first (because there are fewer indels than SNPs) we set INDEL recalibration to
## be applied first to the input VCF, while the SNP model is still being built. By
## the time the SNP model is available, the indel-recalibrated file is available to
## serve as input to apply the SNP recalibration. If we did it the other way around,
## we would have to wait until the SNP recal file was available despite the INDEL
## recal file being there already, then apply SNP recalibration, then apply INDEL
## recalibration. This would lead to a longer wall clock time for complete workflow
## execution. Wiring the INDEL recalibration to be applied first solves the problem.
##
## Cromwell version support
## - Successfully tested on v31
## - Does not work on versions < v23 due to output syntax
##
## Runtime parameters are optimized for Broad's Google Cloud Platform implementation.
## For program versions, see docker containers.
##
## LICENSING :
## This script is released under the WDL source code license (BSD-3) (see LICENSE in
## https://github.com/broadinstitute/wdl). Note however that the programs it calls may
## be subject to different licenses. Users are responsible for checking that they are
## authorized to run all programs before running this script. Please see the docker
## page at https://hub.docker.com/r/broadinstitute/genomes-in-the-cloud/ for detailed
## licensing information pertaining to the included programs.
workflow JointGenotyping {
# Input Sample
String callset_name
File sample_name_map
# Reference and Resources
File ref_fasta
File ref_fasta_index
File ref_dict
File dbsnp_vcf
File dbsnp_vcf_index
Array[String] snp_recalibration_tranche_values
Array[String] snp_recalibration_annotation_values
Array[String] indel_recalibration_tranche_values
Array[String] indel_recalibration_annotation_values
File eval_interval_list
File hapmap_resource_vcf
File hapmap_resource_vcf_index
File omni_resource_vcf
File omni_resource_vcf_index
File one_thousand_genomes_resource_vcf
File one_thousand_genomes_resource_vcf_index
File mills_resource_vcf
File mills_resource_vcf_index
File axiomPoly_resource_vcf
File axiomPoly_resource_vcf_index
File dbsnp_resource_vcf = dbsnp_vcf
File dbsnp_resource_vcf_index = dbsnp_vcf_index
File unpadded_intervals_file
# Runtime attributes
String? gatk_docker_override
String gatk_docker = select_first([gatk_docker_override, "broadinstitute/gatk:4.1.0.0"])
String? gatk_path_override
String gatk_path = select_first([gatk_path_override, "/gatk/gatk"])
Int? small_disk_override
Int small_disk = select_first([small_disk_override, "100"])
Int? medium_disk_override
Int medium_disk = select_first([medium_disk_override, "200"])
Int? large_disk_override
Int large_disk = select_first([large_disk_override, "300"])
Int? huge_disk_override
Int huge_disk = select_first([huge_disk_override, "400"])
String? preemptible_tries_override
Int preemptible_tries = select_first([preemptible_tries_override, "3"])
# ExcessHet is a phred-scaled p-value. We want a cutoff of anything more extreme
# than a z-score of -4.5 which is a p-value of 3.4e-06, which phred-scaled is 54.69
Float excess_het_threshold = 54.69
Float snp_filter_level
Float indel_filter_level
Int SNP_VQSR_downsampleFactor
Int num_of_original_intervals = length(read_lines(unpadded_intervals_file))
Int num_gvcfs = length(read_lines(sample_name_map))
# Make a 2.5:1 interval number to samples in callset ratio interval list
Int possible_merge_count = floor(num_of_original_intervals / num_gvcfs / 2.5)
Int merge_count = if possible_merge_count > 1 then possible_merge_count else 1
call DynamicallyCombineIntervals {
input:
intervals = unpadded_intervals_file,
merge_count = merge_count,
preemptible_tries = preemptible_tries
}
Array[String] unpadded_intervals = read_lines(DynamicallyCombineIntervals.output_intervals)
scatter (idx in range(length(unpadded_intervals))) {
# the batch_size value was carefully chosen here as it
# is the optimal value for the amount of memory allocated
# within the task; please do not change it without consulting
# the Hellbender (GATK engine) team!
call ImportGVCFs {
input:
sample_name_map = sample_name_map,
interval = unpadded_intervals[idx],
workspace_dir_name = "genomicsdb",
disk_size = medium_disk,
batch_size = 50,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
call GenotypeGVCFs {
input:
workspace_tar = ImportGVCFs.output_genomicsdb,
interval = unpadded_intervals[idx],
output_vcf_filename = "output.vcf.gz",
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
ref_dict = ref_dict,
dbsnp_vcf = dbsnp_vcf,
disk_size = medium_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
call HardFilterAndMakeSitesOnlyVcf {
input:
vcf = GenotypeGVCFs.output_vcf,
vcf_index = GenotypeGVCFs.output_vcf_index,
excess_het_threshold = excess_het_threshold,
variant_filtered_vcf_filename = callset_name + "." + idx + ".variant_filtered.vcf.gz",
sites_only_vcf_filename = callset_name + "." + idx + ".sites_only.variant_filtered.vcf.gz",
disk_size = medium_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
}
call GatherVcfs as SitesOnlyGatherVcf {
input:
input_vcfs_fofn = write_lines(HardFilterAndMakeSitesOnlyVcf.sites_only_vcf),
output_vcf_name = callset_name + ".sites_only.vcf.gz",
disk_size = medium_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
call IndelsVariantRecalibrator {
input:
sites_only_variant_filtered_vcf = SitesOnlyGatherVcf.output_vcf,
sites_only_variant_filtered_vcf_index = SitesOnlyGatherVcf.output_vcf_index,
recalibration_filename = callset_name + ".indels.recal",
tranches_filename = callset_name + ".indels.tranches",
recalibration_tranche_values = indel_recalibration_tranche_values,
recalibration_annotation_values = indel_recalibration_annotation_values,
mills_resource_vcf = mills_resource_vcf,
mills_resource_vcf_index = mills_resource_vcf_index,
axiomPoly_resource_vcf = axiomPoly_resource_vcf,
axiomPoly_resource_vcf_index = axiomPoly_resource_vcf_index,
dbsnp_resource_vcf = dbsnp_resource_vcf,
dbsnp_resource_vcf_index = dbsnp_resource_vcf_index,
disk_size = small_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
if (num_gvcfs > 10000) {
call SNPsVariantRecalibratorCreateModel {
input:
sites_only_variant_filtered_vcf = SitesOnlyGatherVcf.output_vcf,
sites_only_variant_filtered_vcf_index = SitesOnlyGatherVcf.output_vcf_index,
recalibration_filename = callset_name + ".snps.recal",
tranches_filename = callset_name + ".snps.tranches",
recalibration_tranche_values = snp_recalibration_tranche_values,
recalibration_annotation_values = snp_recalibration_annotation_values,
downsampleFactor = SNP_VQSR_downsampleFactor,
model_report_filename = callset_name + ".snps.model.report",
hapmap_resource_vcf = hapmap_resource_vcf,
hapmap_resource_vcf_index = hapmap_resource_vcf_index,
omni_resource_vcf = omni_resource_vcf,
omni_resource_vcf_index = omni_resource_vcf_index,
one_thousand_genomes_resource_vcf = one_thousand_genomes_resource_vcf,
one_thousand_genomes_resource_vcf_index = one_thousand_genomes_resource_vcf_index,
dbsnp_resource_vcf = dbsnp_resource_vcf,
dbsnp_resource_vcf_index = dbsnp_resource_vcf_index,
disk_size = small_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
scatter (idx in range(length(HardFilterAndMakeSitesOnlyVcf.sites_only_vcf))) {
call SNPsVariantRecalibrator as SNPsVariantRecalibratorScattered {
input:
sites_only_variant_filtered_vcf = HardFilterAndMakeSitesOnlyVcf.sites_only_vcf[idx],
sites_only_variant_filtered_vcf_index = HardFilterAndMakeSitesOnlyVcf.sites_only_vcf_index[idx],
recalibration_filename = callset_name + ".snps." + idx + ".recal",
tranches_filename = callset_name + ".snps." + idx + ".tranches",
recalibration_tranche_values = snp_recalibration_tranche_values,
recalibration_annotation_values = snp_recalibration_annotation_values,
model_report = SNPsVariantRecalibratorCreateModel.model_report,
hapmap_resource_vcf = hapmap_resource_vcf,
hapmap_resource_vcf_index = hapmap_resource_vcf_index,
omni_resource_vcf = omni_resource_vcf,
omni_resource_vcf_index = omni_resource_vcf_index,
one_thousand_genomes_resource_vcf = one_thousand_genomes_resource_vcf,
one_thousand_genomes_resource_vcf_index = one_thousand_genomes_resource_vcf_index,
dbsnp_resource_vcf = dbsnp_resource_vcf,
dbsnp_resource_vcf_index = dbsnp_resource_vcf_index,
disk_size = small_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
}
call GatherTranches as SNPGatherTranches {
input:
input_fofn = write_lines(SNPsVariantRecalibratorScattered.tranches),
output_filename = callset_name + ".snps.gathered.tranches",
disk_size = small_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
}
if (num_gvcfs <= 10000){
call SNPsVariantRecalibrator as SNPsVariantRecalibratorClassic {
input:
sites_only_variant_filtered_vcf = SitesOnlyGatherVcf.output_vcf,
sites_only_variant_filtered_vcf_index = SitesOnlyGatherVcf.output_vcf_index,
recalibration_filename = callset_name + ".snps.recal",
tranches_filename = callset_name + ".snps.tranches",
recalibration_tranche_values = snp_recalibration_tranche_values,
recalibration_annotation_values = snp_recalibration_annotation_values,
hapmap_resource_vcf = hapmap_resource_vcf,
hapmap_resource_vcf_index = hapmap_resource_vcf_index,
omni_resource_vcf = omni_resource_vcf,
omni_resource_vcf_index = omni_resource_vcf_index,
one_thousand_genomes_resource_vcf = one_thousand_genomes_resource_vcf,
one_thousand_genomes_resource_vcf_index = one_thousand_genomes_resource_vcf_index,
dbsnp_resource_vcf = dbsnp_resource_vcf,
dbsnp_resource_vcf_index = dbsnp_resource_vcf_index,
disk_size = small_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
}
# For small callsets (fewer than 1000 samples) we can gather the VCF shards and collect metrics directly.
# For anything larger, we need to keep the VCF sharded and gather metrics collected from them.
Boolean is_small_callset = num_gvcfs <= 1000
scatter (idx in range(length(HardFilterAndMakeSitesOnlyVcf.variant_filtered_vcf))) {
call ApplyRecalibration {
input:
recalibrated_vcf_filename = callset_name + ".filtered." + idx + ".vcf.gz",
input_vcf = HardFilterAndMakeSitesOnlyVcf.variant_filtered_vcf[idx],
input_vcf_index = HardFilterAndMakeSitesOnlyVcf.variant_filtered_vcf_index[idx],
indels_recalibration = IndelsVariantRecalibrator.recalibration,
indels_recalibration_index = IndelsVariantRecalibrator.recalibration_index,
indels_tranches = IndelsVariantRecalibrator.tranches,
snps_recalibration = if defined(SNPsVariantRecalibratorScattered.recalibration) then select_first([SNPsVariantRecalibratorScattered.recalibration])[idx] else select_first([SNPsVariantRecalibratorClassic.recalibration]),
snps_recalibration_index = if defined(SNPsVariantRecalibratorScattered.recalibration_index) then select_first([SNPsVariantRecalibratorScattered.recalibration_index])[idx] else select_first([SNPsVariantRecalibratorClassic.recalibration_index]),
snps_tranches = select_first([SNPGatherTranches.tranches, SNPsVariantRecalibratorClassic.tranches]),
indel_filter_level = indel_filter_level,
snp_filter_level = snp_filter_level,
disk_size = medium_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
# for large callsets we need to collect metrics from the shards and gather them later
if (!is_small_callset) {
call CollectVariantCallingMetrics as CollectMetricsSharded {
input:
input_vcf = ApplyRecalibration.recalibrated_vcf,
input_vcf_index = ApplyRecalibration.recalibrated_vcf_index,
metrics_filename_prefix = callset_name + "." + idx,
dbsnp_vcf = dbsnp_vcf,
dbsnp_vcf_index = dbsnp_vcf_index,
interval_list = eval_interval_list,
ref_dict = ref_dict,
disk_size = medium_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
}
}
# for small callsets we can gather the VCF shards and then collect metrics on it
if (is_small_callset) {
call GatherVcfs as FinalGatherVcf {
input:
input_vcfs_fofn = write_lines(ApplyRecalibration.recalibrated_vcf),
output_vcf_name = callset_name + ".vcf.gz",
disk_size = huge_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
call CollectVariantCallingMetrics as CollectMetricsOnFullVcf {
input:
input_vcf = FinalGatherVcf.output_vcf,
input_vcf_index = FinalGatherVcf.output_vcf_index,
metrics_filename_prefix = callset_name,
dbsnp_vcf = dbsnp_vcf,
dbsnp_vcf_index = dbsnp_vcf_index,
interval_list = eval_interval_list,
ref_dict = ref_dict,
disk_size = large_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
}
# for large callsets we still need to gather the sharded metrics
if (!is_small_callset) {
call GatherMetrics {
input:
input_details_fofn = write_lines(select_all(CollectMetricsSharded.detail_metrics_file)),
input_summaries_fofn = write_lines(select_all(CollectMetricsSharded.summary_metrics_file)),
output_prefix = callset_name,
disk_size = medium_disk,
docker = gatk_docker,
gatk_path = gatk_path,
preemptible_tries = preemptible_tries
}
}
output {
# outputs from the small callset path through the wdl
File? output_vcf = FinalGatherVcf.output_vcf
File? output_vcf_index = FinalGatherVcf.output_vcf_index
# select metrics from the small callset path and the large callset path
File detail_metrics_file = select_first([CollectMetricsOnFullVcf.detail_metrics_file, GatherMetrics.detail_metrics_file])
File summary_metrics_file = select_first([CollectMetricsOnFullVcf.summary_metrics_file, GatherMetrics.summary_metrics_file])
# output the interval list generated/used by this run workflow
File output_intervals = DynamicallyCombineIntervals.output_intervals
}
}
task GetNumberOfSamples {
File sample_name_map
String docker
Int preemptible_tries
command <<<
wc -l ${sample_name_map} | awk '{print $1}'
>>>
runtime {
docker: docker
memory: "1 GB"
preemptible: preemptible_tries
}
output {
Int sample_count = read_int(stdout())
}
}
task ImportGVCFs {
File sample_name_map
String interval
String workspace_dir_name
String gatk_path
String docker
Int disk_size
Int preemptible_tries
Int batch_size
command <<<
set -e
rm -rf ${workspace_dir_name}
# The memory setting here is very important and must be several GB lower
# than the total memory allocated to the VM because this tool uses
# a significant amount of non-heap memory for native libraries.
# Also, testing has shown that the multithreaded reader initialization
# does not scale well beyond 5 threads, so don't increase beyond that.
${gatk_path} --java-options "-Xmx4g -Xms4g" \
GenomicsDBImport \
--genomicsdb-workspace-path ${workspace_dir_name} \
--batch-size ${batch_size} \
-L ${interval} \
--sample-name-map ${sample_name_map} \
--reader-threads 5 \
-ip 500
tar -cf ${workspace_dir_name}.tar ${workspace_dir_name}
>>>
runtime {
docker: docker
memory: "7 GB"
cpu: "2"
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
}
output {
File output_genomicsdb = "${workspace_dir_name}.tar"
}
}
task GenotypeGVCFs {
File workspace_tar
String interval
String output_vcf_filename
String gatk_path
File ref_fasta
File ref_fasta_index
File ref_dict
String dbsnp_vcf
String docker
Int disk_size
Int preemptible_tries
command <<<
set -e
tar -xf ${workspace_tar}
WORKSPACE=$( basename ${workspace_tar} .tar)
${gatk_path} --java-options "-Xmx5g -Xms5g" \
GenotypeGVCFs \
-R ${ref_fasta} \
-O ${output_vcf_filename} \
-D ${dbsnp_vcf} \
-G StandardAnnotation \
--only-output-calls-starting-in-intervals \
--use-new-qual-calculator \
-V gendb://$WORKSPACE \
-L ${interval}
>>>
runtime {
docker: docker
memory: "7 GB"
cpu: "2"
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
}
output {
File output_vcf = "${output_vcf_filename}"
File output_vcf_index = "${output_vcf_filename}.tbi"
}
}
task HardFilterAndMakeSitesOnlyVcf {
File vcf
File vcf_index
Float excess_het_threshold
String variant_filtered_vcf_filename
String sites_only_vcf_filename
String gatk_path
String docker
Int disk_size
Int preemptible_tries
command {
set -e
${gatk_path} --java-options "-Xmx3g -Xms3g" \
VariantFiltration \
--filter-expression "ExcessHet > ${excess_het_threshold}" \
--filter-name ExcessHet \
-O ${variant_filtered_vcf_filename} \
-V ${vcf}
${gatk_path} --java-options "-Xmx3g -Xms3g" \
MakeSitesOnlyVcf \
--INPUT ${variant_filtered_vcf_filename} \
--OUTPUT ${sites_only_vcf_filename}
}
runtime {
docker: docker
memory: "3.5 GB"
cpu: "1"
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
}
output {
File variant_filtered_vcf = "${variant_filtered_vcf_filename}"
File variant_filtered_vcf_index = "${variant_filtered_vcf_filename}.tbi"
File sites_only_vcf = "${sites_only_vcf_filename}"
File sites_only_vcf_index = "${sites_only_vcf_filename}.tbi"
}
}
task IndelsVariantRecalibrator {
String recalibration_filename
String tranches_filename
Array[String] recalibration_tranche_values
Array[String] recalibration_annotation_values
File sites_only_variant_filtered_vcf
File sites_only_variant_filtered_vcf_index
File mills_resource_vcf
File axiomPoly_resource_vcf
File dbsnp_resource_vcf
File mills_resource_vcf_index
File axiomPoly_resource_vcf_index
File dbsnp_resource_vcf_index
String gatk_path
String docker
Int disk_size
Int preemptible_tries
command {
${gatk_path} --java-options "-Xmx24g -Xms24g" \
VariantRecalibrator \
-V ${sites_only_variant_filtered_vcf} \
-O ${recalibration_filename} \
--tranches-file ${tranches_filename} \
--trust-all-polymorphic \
-tranche ${sep=' -tranche ' recalibration_tranche_values} \
-an ${sep=' -an ' recalibration_annotation_values} \
-mode INDEL \
--max-gaussians 4 \
--resource:mills,known=false,training=true,truth=true,prior=12 ${mills_resource_vcf} \
--resource:axiomPoly,known=false,training=true,truth=false,prior=10 ${axiomPoly_resource_vcf} \
--resource:dbsnp,known=true,training=false,truth=false,prior=2 ${dbsnp_resource_vcf}
}
runtime {
docker: docker
memory: "26 GB"
cpu: "2"
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
}
output {
File recalibration = "${recalibration_filename}"
File recalibration_index = "${recalibration_filename}.idx"
File tranches = "${tranches_filename}"
}
}
task SNPsVariantRecalibratorCreateModel {
String recalibration_filename
String tranches_filename
Int downsampleFactor
String model_report_filename
Array[String] recalibration_tranche_values
Array[String] recalibration_annotation_values
File sites_only_variant_filtered_vcf
File sites_only_variant_filtered_vcf_index
File hapmap_resource_vcf
File omni_resource_vcf
File one_thousand_genomes_resource_vcf
File dbsnp_resource_vcf
File hapmap_resource_vcf_index
File omni_resource_vcf_index
File one_thousand_genomes_resource_vcf_index
File dbsnp_resource_vcf_index
String gatk_path
String docker
Int disk_size
Int preemptible_tries
command {
${gatk_path} --java-options "-Xmx100g -Xms100g" \
VariantRecalibrator \
-V ${sites_only_variant_filtered_vcf} \
-O ${recalibration_filename} \
--tranches-file ${tranches_filename} \
--trust-all-polymorphic \
-tranche ${sep=' -tranche ' recalibration_tranche_values} \
-an ${sep=' -an ' recalibration_annotation_values} \
-mode SNP \
--sample-every-Nth-variant ${downsampleFactor} \
--output-model ${model_report_filename} \
--max-gaussians 6 \
--resource:hapmap,known=false,training=true,truth=true,prior=15 ${hapmap_resource_vcf} \
--resource:omni,known=false,training=true,truth=true,prior=12 ${omni_resource_vcf} \
--resource:1000G,known=false,training=true,truth=false,prior=10 ${one_thousand_genomes_resource_vcf} \
--resource:dbsnp,known=true,training=false,truth=false,prior=7 ${dbsnp_resource_vcf}
}
runtime {
docker: docker
memory: "104 GB"
cpu: "2"
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
}
output {
File model_report = "${model_report_filename}"
}
}
task SNPsVariantRecalibrator {
String recalibration_filename
String tranches_filename
File? model_report
Array[String] recalibration_tranche_values
Array[String] recalibration_annotation_values
File sites_only_variant_filtered_vcf
File sites_only_variant_filtered_vcf_index
File hapmap_resource_vcf
File omni_resource_vcf
File one_thousand_genomes_resource_vcf
File dbsnp_resource_vcf
File hapmap_resource_vcf_index
File omni_resource_vcf_index
File one_thousand_genomes_resource_vcf_index
File dbsnp_resource_vcf_index
String gatk_path
String docker
Int? machine_mem_gb
Int auto_mem = ceil(2*size(sites_only_variant_filtered_vcf, "GB" ))
Int machine_mem = select_first([machine_mem_gb, if auto_mem < 7 then 7 else auto_mem])
Int disk_size
Int preemptible_tries
command {
${gatk_path} --java-options "-Xmx3g -Xms3g" \
VariantRecalibrator \
-V ${sites_only_variant_filtered_vcf} \
-O ${recalibration_filename} \
--tranches-file ${tranches_filename} \
--trust-all-polymorphic \
-tranche ${sep=' -tranche ' recalibration_tranche_values} \
-an ${sep=' -an ' recalibration_annotation_values} \
-mode SNP \
${"--input-model " + model_report + " --output-tranches-for-scatter "} \
--max-gaussians 6 \
--resource:hapmap,known=false,training=true,truth=true,prior=15 ${hapmap_resource_vcf} \
--resource:omni,known=false,training=true,truth=true,prior=12 ${omni_resource_vcf} \
--resource:1000G,known=false,training=true,truth=false,prior=10 ${one_thousand_genomes_resource_vcf} \
--resource:dbsnp,known=true,training=false,truth=false,prior=7 ${dbsnp_resource_vcf}
}
runtime {
docker: docker
memory: machine_mem + " GB"
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
}
output {
File recalibration = "${recalibration_filename}"
File recalibration_index = "${recalibration_filename}.idx"
File tranches = "${tranches_filename}"
}
}
task GatherTranches {
File input_fofn
String output_filename
String gatk_path
String docker
Int disk_size
Int preemptible_tries
command <<<
set -e
set -o pipefail
# this is here to deal with the JES bug where commands may be run twice
rm -rf tranches
mkdir tranches
RETRY_LIMIT=5
count=0
until cat ${input_fofn} | /google-cloud-sdk/bin/gsutil -m cp -L cp.log -c -I tranches/; do
sleep 1
((count++)) && ((count >= $RETRY_LIMIT)) && break
done
if [ "$count" -ge "$RETRY_LIMIT" ]; then
echo 'Could not copy all the tranches from the cloud' && exit 1
fi
cat ${input_fofn} | rev | cut -d '/' -f 1 | rev | awk '{print "tranches/" $1}' > inputs.list
${gatk_path} --java-options "-Xmx6g -Xms6g" \
GatherTranches \
--input inputs.list \
--output ${output_filename}
>>>
runtime {
docker: docker
memory: "7 GB"
cpu: "2"
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
}
output {
File tranches = "${output_filename}"
}
}
task ApplyRecalibration {
String recalibrated_vcf_filename
File input_vcf
File input_vcf_index
File indels_recalibration
File indels_recalibration_index
File indels_tranches
File snps_recalibration
File snps_recalibration_index
File snps_tranches
Float indel_filter_level
Float snp_filter_level
String gatk_path
String docker
Int disk_size
Int preemptible_tries
command {
set -e
${gatk_path} --java-options "-Xmx5g -Xms5g" \
ApplyVQSR \
-O tmp.indel.recalibrated.vcf \
-V ${input_vcf} \
--recal-file ${indels_recalibration} \
--tranches-file ${indels_tranches} \
--truth-sensitivity-filter-level ${indel_filter_level} \
--create-output-variant-index true \
-mode INDEL
${gatk_path} --java-options "-Xmx5g -Xms5g" \
ApplyVQSR \
-O ${recalibrated_vcf_filename} \
-V tmp.indel.recalibrated.vcf \
--recal-file ${snps_recalibration} \
--tranches-file ${snps_tranches} \
--truth-sensitivity-filter-level ${snp_filter_level} \
--create-output-variant-index true \
-mode SNP
}
runtime {
docker: docker
memory: "7 GB"
cpu: "1"
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
}
output {
File recalibrated_vcf = "${recalibrated_vcf_filename}"
File recalibrated_vcf_index = "${recalibrated_vcf_filename}.tbi"
}
}
task GatherVcfs {
File input_vcfs_fofn
String output_vcf_name
String gatk_path
String docker
Int disk_size
Int preemptible_tries
command <<<
set -e
# Now using NIO to localize the vcfs but the input file must have a ".list" extension
mv ${input_vcfs_fofn} inputs.list
# --ignore-safety-checks makes a big performance difference so we include it in our invocation.
# This argument disables expensive checks that the file headers contain the same set of
# genotyped samples and that files are in order by position of first record.
${gatk_path} --java-options "-Xmx6g -Xms6g" \
GatherVcfsCloud \
--ignore-safety-checks \
--gather-type BLOCK \
--input inputs.list \
--output ${output_vcf_name}
${gatk_path} --java-options "-Xmx6g -Xms6g" \
IndexFeatureFile \
--feature-file ${output_vcf_name}
>>>
runtime {
docker: docker
memory: "7 GB"
cpu: "1"
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
}
output {
File output_vcf = "${output_vcf_name}"
File output_vcf_index = "${output_vcf_name}.tbi"
}
}
task CollectVariantCallingMetrics {
File input_vcf
File input_vcf_index
String metrics_filename_prefix
File dbsnp_vcf
File dbsnp_vcf_index
File interval_list
File ref_dict
String gatk_path
String docker
Int disk_size
Int preemptible_tries
command {
${gatk_path} --java-options "-Xmx6g -Xms6g" \
CollectVariantCallingMetrics \
--INPUT ${input_vcf} \
--DBSNP ${dbsnp_vcf} \
--SEQUENCE_DICTIONARY ${ref_dict} \
--OUTPUT ${metrics_filename_prefix} \
--THREAD_COUNT 8 \
--TARGET_INTERVALS ${interval_list}
}
output {
File detail_metrics_file = "${metrics_filename_prefix}.variant_calling_detail_metrics"
File summary_metrics_file = "${metrics_filename_prefix}.variant_calling_summary_metrics"
}
runtime {
docker: docker
memory: "7 GB"
cpu: 2
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
}
}
task GatherMetrics {
File input_details_fofn
File input_summaries_fofn
String output_prefix
String gatk_path
String docker
Int disk_size
Int preemptible_tries
command <<<
set -e
set -o pipefail
# this is here to deal with the JES bug where commands may be run twice
rm -rf metrics
mkdir metrics
RETRY_LIMIT=5
count=0
until cat ${input_details_fofn} | /google-cloud-sdk/bin/gsutil -m cp -L cp.log -c -I metrics/; do
sleep 1
((count++)) && ((count >= $RETRY_LIMIT)) && break
done
if [ "$count" -ge "$RETRY_LIMIT" ]; then
echo 'Could not copy all the metrics from the cloud' && exit 1
fi
count=0
until cat ${input_summaries_fofn} | /google-cloud-sdk/bin/gsutil -m cp -L cp.log -c -I metrics/; do
sleep 1
((count++)) && ((count >= $RETRY_LIMIT)) && break
done
if [ "$count" -ge "$RETRY_LIMIT" ]; then
echo 'Could not copy all the metrics from the cloud' && exit 1
fi
INPUT=`cat ${input_details_fofn} | rev | cut -d '/' -f 1 | rev | sed s/.variant_calling_detail_metrics//g | awk '{printf("-I=metrics/%s ", $1)}'`
${gatk_path} --java-options "-Xmx2g -Xms2g" \
AccumulateVariantCallingMetrics \
$INPUT \
-O ${output_prefix}
>>>
runtime {
docker: docker
memory: "3 GB"
cpu: "1"
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
}
output {
File detail_metrics_file = "${output_prefix}.variant_calling_detail_metrics"
File summary_metrics_file = "${output_prefix}.variant_calling_summary_metrics"
}
}
task DynamicallyCombineIntervals {
File intervals
Int merge_count
Int preemptible_tries
command {
python << CODE
def parse_interval(interval):
colon_split = interval.split(":")
chromosome = colon_split[0]
dash_split = colon_split[1].split("-")
start = int(dash_split[0])
end = int(dash_split[1])
return chromosome, start, end
def add_interval(chr, start, end):
lines_to_write.append(chr + ":" + str(start) + "-" + str(end))
return chr, start, end
count = 0
chain_count = ${merge_count}
l_chr, l_start, l_end = "", 0, 0
lines_to_write = []
with open("${intervals}") as f:
with open("out.intervals", "w") as f1:
for line in f.readlines():
# initialization
if count == 0:
w_chr, w_start, w_end = parse_interval(line)
count = 1
continue
# reached number to combine, so spit out and start over
if count == chain_count:
l_char, l_start, l_end = add_interval(w_chr, w_start, w_end)
w_chr, w_start, w_end = parse_interval(line)
count = 1
continue
c_chr, c_start, c_end = parse_interval(line)
# if adjacent keep the chain going
if c_chr == w_chr and c_start == w_end + 1:
w_end = c_end
count += 1
continue
# not adjacent, end here and start a new chain
else:
l_char, l_start, l_end = add_interval(w_chr, w_start, w_end)
w_chr, w_start, w_end = parse_interval(line)
count = 1
if l_char != w_chr or l_start != w_start or l_end != w_end:
add_interval(w_chr, w_start, w_end)
f1.writelines("\n".join(lines_to_write))
CODE
}
runtime {
memory: "3 GB"
preemptible: preemptible_tries
docker: "python:2.7"
}