-
Notifications
You must be signed in to change notification settings - Fork 0
/
PairedEndSingleSampleWf.wdl
1627 lines (1479 loc) · 56.2 KB
/
PairedEndSingleSampleWf.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, 2017
##
## This WDL pipeline implements data pre-processing and initial variant calling (GVCF
## generation) according to the GATK Best Practices (June 2016) for germline SNP and
## Indel discovery in human whole-genome sequencing (WGS) data.
##
## Requirements/expectations :
## - Human whole-genome pair-end sequencing data in unmapped BAM (uBAM) format
## - One or more read groups, one per uBAM file, all belonging to a single sample (SM)
## - Input uBAM files must additionally comply with the following requirements:
## - - filenames all have the same suffix (we use ".unmapped.bam")
## - - files must pass validation by ValidateSamFile
## - - reads are provided in query-sorted order
## - - all reads must have an RG tag
## - GVCF output names must end in ".g.vcf.gz"
## - Reference genome must be Hg38 with ALT contigs
##
## 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 DEFINITION
workflow PairedEndSingleSampleWorkflow {
File contamination_sites_ud
File contamination_sites_bed
File contamination_sites_mu
File? fingerprint_genotypes_file
File? fingerprint_genotypes_index
File? haplotype_database_file
File wgs_evaluation_interval_list
File wgs_coverage_interval_list
String sample_name
String base_file_name
String final_gvcf_base_name
Array[File] flowcell_unmapped_bams
String unmapped_bam_suffix
File wgs_calling_interval_list
Int haplotype_scatter_count
Int break_bands_at_multiples_of
Int? read_length
File ref_fasta
File ref_fasta_index
File ref_dict
File ref_alt
File ref_bwt
File ref_sa
File ref_amb
File ref_ann
File ref_pac
File dbSNP_vcf
File dbSNP_vcf_index
Array[File] known_indels_sites_VCFs
Array[File] known_indels_sites_indices
Int preemptible_tries
Int agg_preemptible_tries
# Optional input to increase all disk sizes in case of outlier sample with strange size behavior
Int? increase_disk_size
# Some tasks need wiggle room, and we also need to add a small amount of disk to prevent getting a
# Cromwell error from asking for 0 disk when the input is less than 1GB
Int additional_disk = select_first([increase_disk_size, 20])
# Germline single sample GVCFs shouldn't get bigger even when the input bam is bigger (after a certain size)
Int GVCF_disk_size = select_first([increase_disk_size, 30])
# Sometimes the output is larger than the input, or a task can spill to disk. In these cases we need to account for the
# input (1) and the output (1.5) or the input(1), the output(1), and spillage (.5).
Float bwa_disk_multiplier = 2.5
# SortSam spills to disk a lot more because we are only store 300000 records in RAM now because its faster for our data
# so it needs more disk space. Also it spills to disk in an uncompressed format so we need to account for that with a
# larger multiplier
Float sort_sam_disk_multiplier = 3.25
# Mark Duplicates takes in as input readgroup bams and outputs a slightly smaller aggregated bam. Giving .25 as wiggleroom
Float md_disk_multiplier = 2.25
# ValidateSamFile runs out of memory in mate validation on crazy edge case data, so we want to skip the mate validation
# in those cases. These values set the thresholds for what is considered outside the normal realm of "reasonable" data.
Float max_duplication_in_reasonable_sample = 0.30
Float max_chimerism_in_reasonable_sample = 0.15
String bwa_commandline="bwa mem -K 100000000 -p -v 3 -t 16 -Y $bash_ref_fasta"
String recalibrated_bam_basename = base_file_name + ".aligned.duplicates_marked.recalibrated"
Int compression_level = 2
# Get the version of BWA to include in the PG record in the header of the BAM produced
# by MergeBamAlignment.
call GetBwaVersion
# Get the size of the standard reference files as well as the additional reference files needed for BWA
Float ref_size = size(ref_fasta, "GB") + size(ref_fasta_index, "GB") + size(ref_dict, "GB")
Float bwa_ref_size = ref_size + size(ref_alt, "GB") + size(ref_amb, "GB") + size(ref_ann, "GB") + size(ref_bwt, "GB") + size(ref_pac, "GB") + size(ref_sa, "GB")
Float dbsnp_size = size(dbSNP_vcf, "GB")
# Align flowcell-level unmapped input bams in parallel
scatter (unmapped_bam in flowcell_unmapped_bams) {
Float unmapped_bam_size = size(unmapped_bam, "GB")
String unmapped_bam_basename = basename(unmapped_bam, unmapped_bam_suffix)
# QC the unmapped BAM
call CollectQualityYieldMetrics {
input:
input_bam = unmapped_bam,
metrics_filename = unmapped_bam_basename + ".unmapped.quality_yield_metrics",
disk_size = unmapped_bam_size + additional_disk,
preemptible_tries = preemptible_tries
}
# Map reads to reference
call SamToFastqAndBwaMemAndMba {
input:
input_bam = unmapped_bam,
bwa_commandline = bwa_commandline,
output_bam_basename = unmapped_bam_basename + ".aligned.unsorted",
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
ref_dict = ref_dict,
ref_alt = ref_alt,
ref_bwt = ref_bwt,
ref_amb = ref_amb,
ref_ann = ref_ann,
ref_pac = ref_pac,
ref_sa = ref_sa,
bwa_version = GetBwaVersion.version,
# The merged bam can be bigger than only the aligned bam,
# so account for the output size by multiplying the input size by 2.75.
disk_size = unmapped_bam_size + bwa_ref_size + (bwa_disk_multiplier * unmapped_bam_size) + additional_disk,
compression_level = compression_level,
preemptible_tries = preemptible_tries
}
Float mapped_bam_size = size(SamToFastqAndBwaMemAndMba.output_bam, "GB")
# QC the aligned but unsorted readgroup BAM
# no reference as the input here is unsorted, providing a reference would cause an error
call CollectUnsortedReadgroupBamQualityMetrics {
input:
input_bam = SamToFastqAndBwaMemAndMba.output_bam,
output_bam_prefix = unmapped_bam_basename + ".readgroup",
disk_size = mapped_bam_size + additional_disk,
preemptible_tries = preemptible_tries
}
}
# Sum the read group bam sizes to approximate the aggregated bam size
call SumFloats {
input:
sizes = mapped_bam_size,
preemptible_tries = preemptible_tries
}
# MarkDuplicates and SortSam currently take too long for preemptibles if the input data is too large
Float gb_size_cutoff_for_preemptibles = 110.0
Boolean data_too_large_for_preemptibles = SumFloats.total_size > gb_size_cutoff_for_preemptibles
# Aggregate aligned+merged flowcell BAM files and mark duplicates
# We take advantage of the tool's ability to take multiple BAM inputs and write out a single output
# to avoid having to spend time just merging BAM files.
call MarkDuplicates {
input:
input_bams = SamToFastqAndBwaMemAndMba.output_bam,
output_bam_basename = base_file_name + ".aligned.unsorted.duplicates_marked",
metrics_filename = base_file_name + ".duplicate_metrics",
# The merged bam will be smaller than the sum of the parts so we need to account for the unmerged inputs
# and the merged output.
disk_size = (md_disk_multiplier * SumFloats.total_size) + additional_disk,
compression_level = compression_level,
preemptible_tries = if data_too_large_for_preemptibles then 0 else agg_preemptible_tries
}
Float agg_bam_size = size(MarkDuplicates.output_bam, "GB")
# Sort aggregated+deduped BAM file and fix tags
call SortSam as SortSampleBam {
input:
input_bam = MarkDuplicates.output_bam,
output_bam_basename = base_file_name + ".aligned.duplicate_marked.sorted",
# This task spills to disk so we need space for the input bam, the output bam, and any spillage.
disk_size = (sort_sam_disk_multiplier * agg_bam_size) + additional_disk,
compression_level = compression_level,
preemptible_tries = if data_too_large_for_preemptibles then 0 else agg_preemptible_tries
}
if (defined(haplotype_database_file)) {
# Check identity of fingerprints across readgroups
call CrossCheckFingerprints {
input:
input_bams = SortSampleBam.output_bam,
input_bam_indexes = SortSampleBam.output_bam_index,
haplotype_database_file = haplotype_database_file,
metrics_filename = base_file_name + ".crosscheck",
disk_size = agg_bam_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
}
# Create list of sequences for scatter-gather parallelization
call CreateSequenceGroupingTSV {
input:
ref_dict = ref_dict,
preemptible_tries = preemptible_tries
}
# Estimate level of cross-sample contamination
call CheckContamination {
input:
input_bam = SortSampleBam.output_bam,
input_bam_index = SortSampleBam.output_bam_index,
contamination_sites_ud = contamination_sites_ud,
contamination_sites_bed = contamination_sites_bed,
contamination_sites_mu = contamination_sites_mu,
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
output_prefix = base_file_name + ".preBqsr",
disk_size = agg_bam_size + ref_size + additional_disk,
preemptible_tries = agg_preemptible_tries,
contamination_underestimation_factor = 0.75
}
# We need disk to localize the sharded input and output due to the scatter for BQSR.
# If we take the number we are scattering by and reduce by 3 we will have enough disk space
# to account for the fact that the data is not split evenly.
Int num_of_bqsr_scatters = length(CreateSequenceGroupingTSV.sequence_grouping)
Int potential_bqsr_divisor = num_of_bqsr_scatters - 10
Int bqsr_divisor = if potential_bqsr_divisor > 1 then potential_bqsr_divisor else 1
# Perform Base Quality Score Recalibration (BQSR) on the sorted BAM in parallel
scatter (subgroup in CreateSequenceGroupingTSV.sequence_grouping) {
# Generate the recalibration model by interval
call BaseRecalibrator {
input:
input_bam = SortSampleBam.output_bam,
recalibration_report_filename = base_file_name + ".recal_data.csv",
sequence_group_interval = subgroup,
dbSNP_vcf = dbSNP_vcf,
dbSNP_vcf_index = dbSNP_vcf_index,
known_indels_sites_VCFs = known_indels_sites_VCFs,
known_indels_sites_indices = known_indels_sites_indices,
ref_dict = ref_dict,
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
# We need disk to localize the sharded bam due to the scatter.
disk_size = agg_bam_size + ref_size + dbsnp_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
}
# Merge the recalibration reports resulting from by-interval recalibration
# The reports are always the same size
call GatherBqsrReports {
input:
input_bqsr_reports = BaseRecalibrator.recalibration_report,
output_report_filename = base_file_name + ".recal_data.csv",
disk_size = additional_disk,
preemptible_tries = preemptible_tries
}
scatter (subgroup in CreateSequenceGroupingTSV.sequence_grouping_with_unmapped) {
# Apply the recalibration model by interval
call ApplyBQSR {
input:
input_bam = SortSampleBam.output_bam,
output_bam_basename = recalibrated_bam_basename,
recalibration_report = GatherBqsrReports.output_bqsr_report,
sequence_group_interval = subgroup,
ref_dict = ref_dict,
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
# We need disk to localize the sharded bam and the sharded output due to the scatter.
disk_size = agg_bam_size + (agg_bam_size / bqsr_divisor) + ref_size + additional_disk,
compression_level = compression_level,
preemptible_tries = agg_preemptible_tries
}
}
# Merge the recalibrated BAM files resulting from by-interval recalibration
call GatherBamFiles {
input:
input_bams = ApplyBQSR.recalibrated_bam,
output_bam_basename = base_file_name,
# Multiply the input bam size by two to account for the input and output
disk_size = (2 * agg_bam_size) + additional_disk,
compression_level = compression_level,
preemptible_tries = agg_preemptible_tries
}
#BQSR bins the qualities which makes a significantly smaller bam
Float binned_qual_bam_size = size(GatherBamFiles.output_bam, "GB")
# QC the final BAM (consolidated after scattered BQSR)
call CollectReadgroupBamQualityMetrics {
input:
input_bam = GatherBamFiles.output_bam,
input_bam_index = GatherBamFiles.output_bam_index,
output_bam_prefix = base_file_name + ".readgroup",
ref_dict = ref_dict,
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
disk_size = binned_qual_bam_size + ref_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
# QC the final BAM some more (no such thing as too much QC)
call CollectAggregationMetrics {
input:
input_bam = GatherBamFiles.output_bam,
input_bam_index = GatherBamFiles.output_bam_index,
output_bam_prefix = base_file_name,
ref_dict = ref_dict,
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
disk_size = binned_qual_bam_size + ref_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
if (defined(haplotype_database_file) && defined(fingerprint_genotypes_file)) {
# Check the sample BAM fingerprint against the sample array
call CheckFingerprint {
input:
input_bam = GatherBamFiles.output_bam,
input_bam_index = GatherBamFiles.output_bam_index,
haplotype_database_file = haplotype_database_file,
genotypes = fingerprint_genotypes_file,
genotypes_index = fingerprint_genotypes_index,
output_basename = base_file_name,
sample = sample_name,
disk_size = binned_qual_bam_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
}
# QC the sample WGS metrics (stringent thresholds)
call CollectWgsMetrics {
input:
input_bam = GatherBamFiles.output_bam,
input_bam_index = GatherBamFiles.output_bam_index,
metrics_filename = base_file_name + ".wgs_metrics",
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
wgs_coverage_interval_list = wgs_coverage_interval_list,
read_length = read_length,
disk_size = binned_qual_bam_size + ref_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
# QC the sample raw WGS metrics (common thresholds)
call CollectRawWgsMetrics {
input:
input_bam = GatherBamFiles.output_bam,
input_bam_index = GatherBamFiles.output_bam_index,
metrics_filename = base_file_name + ".raw_wgs_metrics",
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
wgs_coverage_interval_list = wgs_coverage_interval_list,
read_length = read_length,
disk_size = binned_qual_bam_size + ref_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
# Generate a checksum per readgroup in the final BAM
call CalculateReadGroupChecksum {
input:
input_bam = GatherBamFiles.output_bam,
input_bam_index = GatherBamFiles.output_bam_index,
read_group_md5_filename = recalibrated_bam_basename + ".bam.read_group_md5",
disk_size = binned_qual_bam_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
# Convert the final merged recalibrated BAM file to CRAM format
call ConvertToCram {
input:
input_bam = GatherBamFiles.output_bam,
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
output_basename = base_file_name,
disk_size = (2 * binned_qual_bam_size) + ref_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
Float cram_size = size(ConvertToCram.output_cram, "GB")
# Check whether the data has massively high duplication or chimerism rates
call CheckPreValidation {
input:
duplication_metrics = MarkDuplicates.duplicate_metrics,
chimerism_metrics = CollectAggregationMetrics.alignment_summary_metrics,
max_duplication_in_reasonable_sample = max_duplication_in_reasonable_sample,
max_chimerism_in_reasonable_sample = max_chimerism_in_reasonable_sample,
preemptible_tries = agg_preemptible_tries
}
# Validate the CRAM file
call ValidateSamFile as ValidateCram {
input:
input_bam = ConvertToCram.output_cram,
input_bam_index = ConvertToCram.output_cram_index,
report_filename = base_file_name + ".cram.validation_report",
ref_dict = ref_dict,
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
ignore = ["MISSING_TAG_NM"],
max_output = 1000000000,
is_outlier_data = CheckPreValidation.is_outlier_data,
disk_size = cram_size + ref_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
# Break the calling interval_list into sub-intervals
# Perform variant calling on the sub-intervals, and then gather the results
call ScatterIntervalList {
input:
interval_list = wgs_calling_interval_list,
scatter_count = haplotype_scatter_count,
break_bands_at_multiples_of = break_bands_at_multiples_of
}
# We need disk to localize the sharded input and output due to the scatter for HaplotypeCaller.
# If we take the number we are scattering by and reduce by 20 we will have enough disk space
# to account for the fact that the data is quite uneven across the shards.
Int potential_hc_divisor = ScatterIntervalList.interval_count - 20
Int hc_divisor = if potential_hc_divisor > 1 then potential_hc_divisor else 1
# Call variants in parallel over WGS calling intervals
scatter (index in range(ScatterIntervalList.interval_count)) {
# Generate GVCF by interval
call HaplotypeCaller {
input:
contamination = CheckContamination.contamination,
input_bam = GatherBamFiles.output_bam,
interval_list = ScatterIntervalList.out[index],
gvcf_basename = base_file_name,
ref_dict = ref_dict,
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
# Divide the total output GVCF size and the input bam size to account for the smaller scattered input and output.
disk_size = ((binned_qual_bam_size + GVCF_disk_size) / hc_divisor) + ref_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
}
# Combine by-interval GVCFs into a single sample GVCF file
call MergeVCFs {
input:
input_vcfs = HaplotypeCaller.output_gvcf,
input_vcfs_indexes = HaplotypeCaller.output_gvcf_index,
output_vcf_name = final_gvcf_base_name + ".g.vcf.gz",
disk_size = GVCF_disk_size,
preemptible_tries = agg_preemptible_tries
}
Float gvcf_size = size(MergeVCFs.output_vcf, "GB")
# Validate the GVCF output of HaplotypeCaller
call ValidateGVCF {
input:
input_vcf = MergeVCFs.output_vcf,
input_vcf_index = MergeVCFs.output_vcf_index,
dbSNP_vcf = dbSNP_vcf,
dbSNP_vcf_index = dbSNP_vcf_index,
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
ref_dict = ref_dict,
wgs_calling_interval_list = wgs_calling_interval_list,
disk_size = gvcf_size + ref_size + dbsnp_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
# QC the GVCF
call CollectGvcfCallingMetrics {
input:
input_vcf = MergeVCFs.output_vcf,
input_vcf_index = MergeVCFs.output_vcf_index,
metrics_basename = final_gvcf_base_name,
dbSNP_vcf = dbSNP_vcf,
dbSNP_vcf_index = dbSNP_vcf_index,
ref_dict = ref_dict,
wgs_evaluation_interval_list = wgs_evaluation_interval_list,
disk_size = gvcf_size + dbsnp_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
# Outputs that will be retained when execution is complete
output {
Array[File] quality_yield_metrics = CollectQualityYieldMetrics.metrics
Array[File] unsorted_read_group_base_distribution_by_cycle_pdf = CollectUnsortedReadgroupBamQualityMetrics.base_distribution_by_cycle_pdf
Array[File] unsorted_read_group_base_distribution_by_cycle_metrics = CollectUnsortedReadgroupBamQualityMetrics.base_distribution_by_cycle_metrics
Array[File] unsorted_read_group_insert_size_histogram_pdf = CollectUnsortedReadgroupBamQualityMetrics.insert_size_histogram_pdf
Array[File] unsorted_read_group_insert_size_metrics = CollectUnsortedReadgroupBamQualityMetrics.insert_size_metrics
Array[File] unsorted_read_group_quality_by_cycle_pdf = CollectUnsortedReadgroupBamQualityMetrics.quality_by_cycle_pdf
Array[File] unsorted_read_group_quality_by_cycle_metrics = CollectUnsortedReadgroupBamQualityMetrics.quality_by_cycle_metrics
Array[File] unsorted_read_group_quality_distribution_pdf = CollectUnsortedReadgroupBamQualityMetrics.quality_distribution_pdf
Array[File] unsorted_read_group_quality_distribution_metrics = CollectUnsortedReadgroupBamQualityMetrics.quality_distribution_metrics
File read_group_alignment_summary_metrics = CollectReadgroupBamQualityMetrics.alignment_summary_metrics
File read_group_gc_bias_detail_metrics = CollectReadgroupBamQualityMetrics.gc_bias_detail_metrics
File read_group_gc_bias_pdf = CollectReadgroupBamQualityMetrics.gc_bias_pdf
File read_group_gc_bias_summary_metrics = CollectReadgroupBamQualityMetrics.gc_bias_summary_metrics
File? cross_check_fingerprints_metrics = CrossCheckFingerprints.metrics
File selfSM = CheckContamination.selfSM
Float contamination = CheckContamination.contamination
File calculate_read_group_checksum_md5 = CalculateReadGroupChecksum.md5_file
File agg_alignment_summary_metrics = CollectAggregationMetrics.alignment_summary_metrics
File agg_bait_bias_detail_metrics = CollectAggregationMetrics.bait_bias_detail_metrics
File agg_bait_bias_summary_metrics = CollectAggregationMetrics.bait_bias_summary_metrics
File agg_gc_bias_detail_metrics = CollectAggregationMetrics.gc_bias_detail_metrics
File agg_gc_bias_pdf = CollectAggregationMetrics.gc_bias_pdf
File agg_gc_bias_summary_metrics = CollectAggregationMetrics.gc_bias_summary_metrics
File agg_insert_size_histogram_pdf = CollectAggregationMetrics.insert_size_histogram_pdf
File agg_insert_size_metrics = CollectAggregationMetrics.insert_size_metrics
File agg_pre_adapter_detail_metrics = CollectAggregationMetrics.pre_adapter_detail_metrics
File agg_pre_adapter_summary_metrics = CollectAggregationMetrics.pre_adapter_summary_metrics
File agg_quality_distribution_pdf = CollectAggregationMetrics.quality_distribution_pdf
File agg_quality_distribution_metrics = CollectAggregationMetrics.quality_distribution_metrics
File? fingerprint_summary_metrics = CheckFingerprint.summary_metrics
File? fingerprint_detail_metrics = CheckFingerprint.detail_metrics
File wgs_metrics = CollectWgsMetrics.metrics
File raw_wgs_metrics = CollectRawWgsMetrics.metrics
File gvcf_summary_metrics = CollectGvcfCallingMetrics.summary_metrics
File gvcf_detail_metrics = CollectGvcfCallingMetrics.detail_metrics
File duplicate_metrics = MarkDuplicates.duplicate_metrics
File output_bqsr_reports = GatherBqsrReports.output_bqsr_report
File output_cram = ConvertToCram.output_cram
File output_cram_index = ConvertToCram.output_cram_index
File output_cram_md5 = ConvertToCram.output_cram_md5
File validate_cram_file_report = ValidateCram.report
File output_vcf = MergeVCFs.output_vcf
File output_vcf_index = MergeVCFs.output_vcf_index
}
}
# TASK DEFINITIONS
# Collect sequencing yield quality metrics
task CollectQualityYieldMetrics {
File input_bam
String metrics_filename
Float disk_size
Int preemptible_tries
command {
java -Xms2000m -jar /usr/gitc/picard.jar \
CollectQualityYieldMetrics \
INPUT=${input_bam} \
OQ=true \
OUTPUT=${metrics_filename}
}
runtime {
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
memory: "3 GB"
preemptible: preemptible_tries
}
output {
File metrics = "${metrics_filename}"
}
}
# Get version of BWA
task GetBwaVersion {
command {
# not setting set -o pipefail here because /bwa has a rc=1 and we dont want to allow rc=1 to succeed because
# the sed may also fail with that error and that is something we actually want to fail on.
/usr/gitc/bwa 2>&1 | \
grep -e '^Version' | \
sed 's/Version: //'
}
runtime {
memory: "1 GB"
}
output {
String version = read_string(stdout())
}
}
# Read unmapped BAM, convert on-the-fly to FASTQ and stream to BWA MEM for alignment, then stream to MergeBamAlignment
task SamToFastqAndBwaMemAndMba {
File input_bam
String bwa_commandline
String bwa_version
String output_bam_basename
File ref_fasta
File ref_fasta_index
File ref_dict
# This is the .alt file from bwa-kit (https://github.com/lh3/bwa/tree/master/bwakit),
# listing the reference contigs that are "alternative".
File ref_alt
File ref_amb
File ref_ann
File ref_bwt
File ref_pac
File ref_sa
Float disk_size
Int compression_level
Int preemptible_tries
command <<<
set -o pipefail
set -e
# set the bash variable needed for the command-line
bash_ref_fasta=${ref_fasta}
# if ref_alt has data in it,
if [ -s ${ref_alt} ]; then
java -Xms5000m -jar /usr/gitc/picard.jar \
SamToFastq \
INPUT=${input_bam} \
FASTQ=/dev/stdout \
INTERLEAVE=true \
NON_PF=true | \
/usr/gitc/${bwa_commandline} /dev/stdin - 2> >(tee ${output_bam_basename}.bwa.stderr.log >&2) | \
java -Dsamjdk.compression_level=${compression_level} -Xms3000m -jar /usr/gitc/picard.jar \
MergeBamAlignment \
VALIDATION_STRINGENCY=SILENT \
EXPECTED_ORIENTATIONS=FR \
ATTRIBUTES_TO_RETAIN=X0 \
ATTRIBUTES_TO_REMOVE=NM \
ATTRIBUTES_TO_REMOVE=MD \
ALIGNED_BAM=/dev/stdin \
UNMAPPED_BAM=${input_bam} \
OUTPUT=${output_bam_basename}.bam \
REFERENCE_SEQUENCE=${ref_fasta} \
PAIRED_RUN=true \
SORT_ORDER="unsorted" \
IS_BISULFITE_SEQUENCE=false \
ALIGNED_READS_ONLY=false \
CLIP_ADAPTERS=false \
MAX_RECORDS_IN_RAM=2000000 \
ADD_MATE_CIGAR=true \
MAX_INSERTIONS_OR_DELETIONS=-1 \
PRIMARY_ALIGNMENT_STRATEGY=MostDistant \
PROGRAM_RECORD_ID="bwamem" \
PROGRAM_GROUP_VERSION="${bwa_version}" \
PROGRAM_GROUP_COMMAND_LINE="${bwa_commandline}" \
PROGRAM_GROUP_NAME="bwamem" \
UNMAPPED_READ_STRATEGY=COPY_TO_TAG \
ALIGNER_PROPER_PAIR_FLAGS=true \
UNMAP_CONTAMINANT_READS=true \
ADD_PG_TAG_TO_READS=false
grep -m1 "read .* ALT contigs" ${output_bam_basename}.bwa.stderr.log | \
grep -v "read 0 ALT contigs"
# else ref_alt is empty or could not be found
else
exit 1;
fi
>>>
runtime {
preemptible: preemptible_tries
memory: "14 GB"
cpu: "16"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
}
output {
File output_bam = "${output_bam_basename}.bam"
File bwa_stderr_log = "${output_bam_basename}.bwa.stderr.log"
}
}
# Sort BAM file by coordinate order and fix tag values for NM and UQ
task SortSam {
File input_bam
String output_bam_basename
Int preemptible_tries
Int compression_level
Float disk_size
command {
java -Dsamjdk.compression_level=${compression_level} -Xms4000m -jar /usr/gitc/picard.jar \
SortSam \
INPUT=${input_bam} \
OUTPUT=${output_bam_basename}.bam \
SORT_ORDER="coordinate" \
CREATE_INDEX=true \
CREATE_MD5_FILE=true \
MAX_RECORDS_IN_RAM=300000
}
runtime {
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
cpu: "1"
memory: "5000 MB"
preemptible: preemptible_tries
}
output {
File output_bam = "${output_bam_basename}.bam"
File output_bam_index = "${output_bam_basename}.bai"
File output_bam_md5 = "${output_bam_basename}.bam.md5"
}
}
# Collect base quality and insert size metrics
task CollectUnsortedReadgroupBamQualityMetrics {
File input_bam
String output_bam_prefix
Int preemptible_tries
Float disk_size
command {
java -Xms5000m -jar /usr/gitc/picard.jar \
CollectMultipleMetrics \
INPUT=${input_bam} \
OUTPUT=${output_bam_prefix} \
ASSUME_SORTED=true \
PROGRAM="null" \
PROGRAM="CollectBaseDistributionByCycle" \
PROGRAM="CollectInsertSizeMetrics" \
PROGRAM="MeanQualityByCycle" \
PROGRAM="QualityScoreDistribution" \
METRIC_ACCUMULATION_LEVEL="null" \
METRIC_ACCUMULATION_LEVEL="ALL_READS"
touch ${output_bam_prefix}.insert_size_metrics
touch ${output_bam_prefix}.insert_size_histogram.pdf
}
runtime {
memory: "7 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
preemptible: preemptible_tries
}
output {
File base_distribution_by_cycle_pdf = "${output_bam_prefix}.base_distribution_by_cycle.pdf"
File base_distribution_by_cycle_metrics = "${output_bam_prefix}.base_distribution_by_cycle_metrics"
File insert_size_histogram_pdf = "${output_bam_prefix}.insert_size_histogram.pdf"
File insert_size_metrics = "${output_bam_prefix}.insert_size_metrics"
File quality_by_cycle_pdf = "${output_bam_prefix}.quality_by_cycle.pdf"
File quality_by_cycle_metrics = "${output_bam_prefix}.quality_by_cycle_metrics"
File quality_distribution_pdf = "${output_bam_prefix}.quality_distribution.pdf"
File quality_distribution_metrics = "${output_bam_prefix}.quality_distribution_metrics"
}
}
# Collect alignment summary and GC bias quality metrics
task CollectReadgroupBamQualityMetrics {
File input_bam
File input_bam_index
String output_bam_prefix
File ref_dict
File ref_fasta
File ref_fasta_index
Int preemptible_tries
Float disk_size
command {
java -Xms5000m -jar /usr/gitc/picard.jar \
CollectMultipleMetrics \
INPUT=${input_bam} \
REFERENCE_SEQUENCE=${ref_fasta} \
OUTPUT=${output_bam_prefix} \
ASSUME_SORTED=true \
PROGRAM="null" \
PROGRAM="CollectAlignmentSummaryMetrics" \
PROGRAM="CollectGcBiasMetrics" \
METRIC_ACCUMULATION_LEVEL="null" \
METRIC_ACCUMULATION_LEVEL="READ_GROUP"
}
runtime {
memory: "7 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
preemptible: preemptible_tries
}
output {
File alignment_summary_metrics = "${output_bam_prefix}.alignment_summary_metrics"
File gc_bias_detail_metrics = "${output_bam_prefix}.gc_bias.detail_metrics"
File gc_bias_pdf = "${output_bam_prefix}.gc_bias.pdf"
File gc_bias_summary_metrics = "${output_bam_prefix}.gc_bias.summary_metrics"
}
}
# Collect quality metrics from the aggregated bam
task CollectAggregationMetrics {
File input_bam
File input_bam_index
String output_bam_prefix
File ref_dict
File ref_fasta
File ref_fasta_index
Int preemptible_tries
Float disk_size
command {
java -Xms5000m -jar /usr/gitc/picard.jar \
CollectMultipleMetrics \
INPUT=${input_bam} \
REFERENCE_SEQUENCE=${ref_fasta} \
OUTPUT=${output_bam_prefix} \
ASSUME_SORTED=true \
PROGRAM="null" \
PROGRAM="CollectAlignmentSummaryMetrics" \
PROGRAM="CollectInsertSizeMetrics" \
PROGRAM="CollectSequencingArtifactMetrics" \
PROGRAM="CollectGcBiasMetrics" \
PROGRAM="QualityScoreDistribution" \
METRIC_ACCUMULATION_LEVEL="null" \
METRIC_ACCUMULATION_LEVEL="SAMPLE" \
METRIC_ACCUMULATION_LEVEL="LIBRARY"
touch ${output_bam_prefix}.insert_size_metrics
touch ${output_bam_prefix}.insert_size_histogram.pdf
}
runtime {
memory: "7 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
preemptible: preemptible_tries
}
output {
File alignment_summary_metrics = "${output_bam_prefix}.alignment_summary_metrics"
File bait_bias_detail_metrics = "${output_bam_prefix}.bait_bias_detail_metrics"
File bait_bias_summary_metrics = "${output_bam_prefix}.bait_bias_summary_metrics"
File gc_bias_detail_metrics = "${output_bam_prefix}.gc_bias.detail_metrics"
File gc_bias_pdf = "${output_bam_prefix}.gc_bias.pdf"
File gc_bias_summary_metrics = "${output_bam_prefix}.gc_bias.summary_metrics"
File insert_size_histogram_pdf = "${output_bam_prefix}.insert_size_histogram.pdf"
File insert_size_metrics = "${output_bam_prefix}.insert_size_metrics"
File pre_adapter_detail_metrics = "${output_bam_prefix}.pre_adapter_detail_metrics"
File pre_adapter_summary_metrics = "${output_bam_prefix}.pre_adapter_summary_metrics"
File quality_distribution_pdf = "${output_bam_prefix}.quality_distribution.pdf"
File quality_distribution_metrics = "${output_bam_prefix}.quality_distribution_metrics"
}
}
# Check that the fingerprints of separate readgroups all match
task CrossCheckFingerprints {
Array[File] input_bams
Array[File] input_bam_indexes
File? haplotype_database_file
String metrics_filename
Float disk_size
Int preemptible_tries
command <<<
java -Dsamjdk.buffer_size=131072 \
-XX:GCTimeLimit=50 -XX:GCHeapFreeLimit=10 -Xms2000m \
-jar /usr/gitc/picard.jar \
CrosscheckReadGroupFingerprints \
OUTPUT=${metrics_filename} \
HAPLOTYPE_MAP=${haplotype_database_file} \
EXPECT_ALL_READ_GROUPS_TO_MATCH=true \
INPUT=${sep=' INPUT=' input_bams} \
LOD_THRESHOLD=-20.0
>>>
runtime {
preemptible: preemptible_tries
memory: "2 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
}
output {
File metrics = "${metrics_filename}"
}
}
# Check that the fingerprint of the sample BAM matches the sample array
task CheckFingerprint {
File input_bam
File input_bam_index
String output_basename
File? haplotype_database_file
File? genotypes
File? genotypes_index
String sample
Float disk_size
Int preemptible_tries
command <<<
java -Dsamjdk.buffer_size=131072 \
-XX:GCTimeLimit=50 -XX:GCHeapFreeLimit=10 -Xms1024m \
-jar /usr/gitc/picard.jar \
CheckFingerprint \
INPUT=${input_bam} \
OUTPUT=${output_basename} \
GENOTYPES=${genotypes} \
HAPLOTYPE_MAP=${haplotype_database_file} \
SAMPLE_ALIAS="${sample}" \
IGNORE_READ_GROUPS=true
>>>
runtime {
preemptible: preemptible_tries
memory: "1 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
}
output {
File summary_metrics = "${output_basename}.fingerprinting_summary_metrics"
File detail_metrics = "${output_basename}.fingerprinting_detail_metrics"
}
}
# Mark duplicate reads to avoid counting non-independent observations
task MarkDuplicates {
Array[File] input_bams
String output_bam_basename
String metrics_filename
Float disk_size
Int compression_level
Int preemptible_tries
# The program default for READ_NAME_REGEX is appropriate in nearly every case.
# Sometimes we wish to supply "null" in order to turn off optical duplicate detection
# This can be desirable if you don't mind the estimated library size being wrong and optical duplicate detection is taking >7 days and failing
String? read_name_regex
# Task is assuming query-sorted input so that the Secondary and Supplementary reads get marked correctly
# This works because the output of BWA is query-grouped and therefore, so is the output of MergeBamAlignment.
# While query-grouped isn't actually query-sorted, it's good enough for MarkDuplicates with ASSUME_SORT_ORDER="queryname"
command {
java -Dsamjdk.compression_level=${compression_level} -Xms4000m -jar /usr/gitc/picard.jar \
MarkDuplicates \
INPUT=${sep=' INPUT=' input_bams} \
OUTPUT=${output_bam_basename}.bam \
METRICS_FILE=${metrics_filename} \
VALIDATION_STRINGENCY=SILENT \
${"READ_NAME_REGEX=" + read_name_regex} \
OPTICAL_DUPLICATE_PIXEL_DISTANCE=2500 \
ASSUME_SORT_ORDER="queryname" \
CLEAR_DT="false" \
ADD_PG_TAG_TO_READS=false
}
runtime {
preemptible: preemptible_tries
memory: "7 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
}
output {
File output_bam = "${output_bam_basename}.bam"
File duplicate_metrics = "${metrics_filename}"
}
}
# Generate sets of intervals for scatter-gathering over chromosomes
task CreateSequenceGroupingTSV {
File ref_dict
Int preemptible_tries
# Use python to create the Sequencing Groupings used for BQSR and PrintReads Scatter.
# It outputs to stdout where it is parsed into a wdl Array[Array[String]]
# e.g. [["1"], ["2"], ["3", "4"], ["5"], ["6", "7", "8"]]
command <<<
python <<CODE
with open("${ref_dict}", "r") as ref_dict_file:
sequence_tuple_list = []
longest_sequence = 0
for line in ref_dict_file:
if line.startswith("@SQ"):
line_split = line.split("\t")
# (Sequence_Name, Sequence_Length)
sequence_tuple_list.append((line_split[1].split("SN:")[1], int(line_split[2].split("LN:")[1])))
longest_sequence = sorted(sequence_tuple_list, key=lambda x: x[1], reverse=True)[0][1]
# We are adding this to the intervals because hg38 has contigs named with embedded colons and a bug in GATK strips off
# the last element after a :, so we add this as a sacrificial element.
hg38_protection_tag = ":1+"
# initialize the tsv string with the first sequence
tsv_string = sequence_tuple_list[0][0] + hg38_protection_tag
temp_size = sequence_tuple_list[0][1]
for sequence_tuple in sequence_tuple_list[1:]:
if temp_size + sequence_tuple[1] <= longest_sequence:
temp_size += sequence_tuple[1]
tsv_string += "\t" + sequence_tuple[0] + hg38_protection_tag
else:
tsv_string += "\n" + sequence_tuple[0] + hg38_protection_tag
temp_size = sequence_tuple[1]
# add the unmapped sequences as a separate line to ensure that they are recalibrated as well
with open("sequence_grouping.txt","w") as tsv_file:
tsv_file.write(tsv_string)
tsv_file.close()
tsv_string += '\n' + "unmapped"
with open("sequence_grouping_with_unmapped.txt","w") as tsv_file_with_unmapped:
tsv_file_with_unmapped.write(tsv_string)
tsv_file_with_unmapped.close()