-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSomaticPairedSingleSampleWf.wdl
862 lines (778 loc) · 29.6 KB
/
SomaticPairedSingleSampleWf.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
## 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.
import "SplitLargeRG.wdl" as splitRG
import "CommonTasks.wdl" as commonTasks
# WORKFLOW DEFINITION
workflow SomaticPairedEndSingleSampleWorkflow {
File contamination_sites_ud
File contamination_sites_bed
File contamination_sites_mu
File wgs_coverage_interval_list
String base_file_name
Array[File] flowcell_unmapped_bams
String unmapped_bam_suffix
Int read_length = 250
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
Float cutoff_for_large_rg_in_gb = 20.0
# 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, 5])
# 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
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) {
String version = GetBwaVersion.version
Int addition_disk_re = additional_disk
String recalibrated_bam_basename_re = recalibrated_bam_basename
Float ref_size_re = ref_size
Float bwa_ref_size_re = bwa_ref_size
Float dbsnp_size_re = dbsnp_size
Float unmapped_bam_size = size(unmapped_bam, "GB")
String sub_strip_path = "gs://.*/"
String sub_strip_unmapped = unmapped_bam_suffix + "$"
String sub_sub = sub(sub(unmapped_bam, sub_strip_path, ""), sub_strip_unmapped, "")
if (unmapped_bam_size > cutoff_for_large_rg_in_gb) {
# Split bam into multiple smaller bams,
# map reads to reference and recombine into one bam
call splitRG.SplitLargeRG as SplitRG {
input:
input_bam = unmapped_bam,
bwa_commandline = bwa_commandline,
bwa_version = version,
output_bam_basename = sub_sub + ".aligned.unsorted",
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
ref_dict = ref_dict,
ref_alt = ref_alt,
ref_amb = ref_amb,
ref_ann = ref_ann,
ref_bwt = ref_bwt,
ref_pac = ref_pac,
ref_sa = ref_sa,
additional_disk = additional_disk,
compression_level = compression_level,
preemptible_tries = preemptible_tries,
bwa_ref_size = bwa_ref_size,
disk_multiplier = bwa_disk_multiplier,
unmapped_bam_size = unmapped_bam_size
}
}
if (unmapped_bam_size <= cutoff_for_large_rg_in_gb) {
# Map reads to reference
call commonTasks.SamToFastqAndBwaMemAndMba {
input:
input_bam = unmapped_bam,
bwa_commandline = bwa_commandline,
output_bam_basename = sub_sub + ".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 = 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
}
}
File output_aligned_bam = select_first([SamToFastqAndBwaMemAndMba.output_bam, SplitRG.aligned_bam])
Float mapped_bam_size = size(output_aligned_bam, "GB")
}
# Sum the read group bam sizes to approximate the aggregated bam size
call commonTasks.SumFloats {
input:
sizes = mapped_bam_size,
preemptible_tries = preemptible_tries
}
# 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 = output_aligned_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 = 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 = 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 - 3
Int bqsr_divisor = 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,
input_bai = SortSampleBam.output_bam_index,
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 / bqsr_divisor) + 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,
input_bai = SortSampleBam.output_bam_index,
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 MyGatherBamFiles {
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(MyGatherBamFiles.output_bam, "GB")
# QC the sample WGS metrics (stringent thresholds)
call CollectWgsMetrics {
input:
input_bam = MyGatherBamFiles.output_bam,
input_bam_index = MyGatherBamFiles.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
}
# Generate a checksum per readgroup in the final BAM
call CalculateReadGroupChecksum {
input:
input_bam = MyGatherBamFiles.output_bam,
input_bam_index = MyGatherBamFiles.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
}
# Validate the CRAM file
call ValidateSamFile as ValidateBam {
input:
input_bam = MyGatherBamFiles.output_bam,
input_bam_index = MyGatherBamFiles.output_bam_index,
report_filename = base_file_name + ".bam.validation_report",
ref_dict = ref_dict,
ref_fasta = ref_fasta,
ref_fasta_index = ref_fasta_index,
ignore = ["MISSING_TAG_NM", "INVALID_TAG_NM"],
max_output = 1000000000,
disk_size = (2 * binned_qual_bam_size) + ref_size + additional_disk,
preemptible_tries = agg_preemptible_tries
}
# Outputs that will be retained when execution is complete
output {
File selfSM = CheckContamination.selfSM
File calculate_read_group_checksum_md5 = CalculateReadGroupChecksum.md5_file
File wgs_metrics = CollectWgsMetrics.metrics
File duplicate_metrics = MarkDuplicates.duplicate_metrics
File output_bqsr_reports = GatherBqsrReports.output_bqsr_report
File output_bam = MyGatherBamFiles.output_bam
File output_bam_index = MyGatherBamFiles.output_bam_index
}
}
# TASK DEFINITIONS
# Check the assumption that the final GVCF filename that is going to be used ends with .g.vcf.gz
task CheckFinalVcfExtension {
String vcf_filename
command <<<
python <<CODE
import os
import sys
filename="${vcf_filename}"
if not filename.endswith(".g.vcf.gz"):
raise Exception("input","gvcf output filename must end with '.g.vcf.gz', found %s"%(filename))
sys.exit(1)
CODE
>>>
runtime {
docker: "python:2.7"
memory: "2 GB"
}
output {
String common_suffix=read_string(stdout())
}
}
# 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())
}
}
# 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"
}
}
# 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()
CODE
>>>
runtime {
preemptible: preemptible_tries
docker: "python:2.7"
memory: "2 GB"
}
output {
Array[Array[String]] sequence_grouping = read_tsv("sequence_grouping.txt")
Array[Array[String]] sequence_grouping_with_unmapped = read_tsv("sequence_grouping_with_unmapped.txt")
}
}
# Generate Base Quality Score Recalibration (BQSR) model
task BaseRecalibrator {
File input_bam
File input_bai
String recalibration_report_filename
Array[String] sequence_group_interval
File dbSNP_vcf
File dbSNP_vcf_index
Array[File] known_indels_sites_VCFs
Array[File] known_indels_sites_indices
File ref_dict
File ref_fasta
File ref_fasta_index
Float disk_size
Int preemptible_tries
command {
/usr/gitc/gatk4/gatk-launch --javaOptions "-XX:GCTimeLimit=50 -XX:GCHeapFreeLimit=10 -XX:+PrintFlagsFinal \
-XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+PrintGCDetails \
-Xloggc:gc_log.log -Xms4000m" \
BaseRecalibrator \
-R ${ref_fasta} \
-I ${input_bam} \
--useOriginalQualities \
-O ${recalibration_report_filename} \
-knownSites ${dbSNP_vcf} \
-knownSites ${sep=" -knownSites " known_indels_sites_VCFs} \
-L ${sep=" -L " sequence_group_interval}
}
runtime {
preemptible: preemptible_tries
memory: "6 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
}
output {
File recalibration_report = "${recalibration_report_filename}"
}
}
# Apply Base Quality Score Recalibration (BQSR) model
task ApplyBQSR {
File input_bam
File input_bai
String output_bam_basename
File recalibration_report
Array[String] sequence_group_interval
File ref_dict
File ref_fasta
File ref_fasta_index
Float disk_size
Int compression_level
Int preemptible_tries
command {
/usr/gitc/gatk4/gatk-launch --javaOptions "-XX:+PrintFlagsFinal -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps \
-XX:+PrintGCDetails -Xloggc:gc_log.log \
-XX:GCTimeLimit=50 -XX:GCHeapFreeLimit=10 -Dsamjdk.compression_level=${compression_level} -Xms3000m" \
ApplyBQSR \
--createOutputBamMD5 \
--addOutputSAMProgramRecord \
-R ${ref_fasta} \
-I ${input_bam} \
--useOriginalQualities \
-O ${output_bam_basename}.bam \
-bqsr ${recalibration_report} \
-SQQ 10 -SQQ 20 -SQQ 30 \
-L ${sep=" -L " sequence_group_interval}
}
runtime {
preemptible: preemptible_tries
memory: "3500 MB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
}
output {
File recalibrated_bam = "${output_bam_basename}.bam"
File recalibrated_bam_checksum = "${output_bam_basename}.bam.md5"
}
}
# Combine multiple recalibration tables from scattered BaseRecalibrator runs
task GatherBqsrReports {
Array[File] input_bqsr_reports
String output_report_filename
Int disk_size
Int preemptible_tries
command {
/usr/gitc/gatk4/gatk-launch --javaOptions "-Xms3000m" \
GatherBQSRReports \
-I ${sep=' -I ' input_bqsr_reports} \
-O ${output_report_filename}
}
runtime {
preemptible: preemptible_tries
memory: "3500 MB"
disks: "local-disk " + disk_size + " HDD"
}
output {
File output_bqsr_report = "${output_report_filename}"
}
}
# Combine multiple recalibrated BAM files from scattered ApplyRecalibration runs
task MyGatherBamFiles {
Array[File] input_bams
String output_bam_basename
Float disk_size
Int compression_level
Int preemptible_tries
command {
java -Dsamjdk.compression_level=${compression_level} -Xms2000m -jar /usr/gitc/picard.jar \
GatherBamFiles \
INPUT=${sep=' INPUT=' input_bams} \
OUTPUT=${output_bam_basename}.bam \
CREATE_INDEX=true \
CREATE_MD5_FILE=true
}
runtime {
preemptible: preemptible_tries
memory: "3 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
}
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"
}
}
task ValidateSamFile {
File input_bam
File? input_bam_index
String report_filename
File ref_dict
File ref_fasta
File ref_fasta_index
Int? max_output
Array[String]? ignore
Float disk_size
Int preemptible_tries
command {
java -Xms6000m -jar /usr/gitc/picard.jar \
ValidateSamFile \
INPUT=${input_bam} \
OUTPUT=${report_filename} \
REFERENCE_SEQUENCE=${ref_fasta} \
${"MAX_OUTPUT=" + max_output} \
IGNORE=${default="null" sep=" IGNORE=" ignore} \
MODE=VERBOSE \
IS_BISULFITE_SEQUENCED=false
}
runtime {
preemptible: preemptible_tries
memory: "7 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
}
output {
File report = "${report_filename}"
}
}
# Note these tasks will break if the read lengths in the bam are greater than 250.
task CollectWgsMetrics {
File input_bam
File input_bam_index
String metrics_filename
File wgs_coverage_interval_list
File ref_fasta
File ref_fasta_index
Int read_length
Float disk_size
Int preemptible_tries
command {
java -Xms2000m -jar /usr/gitc/picard.jar \
CollectWgsMetrics \
INPUT=${input_bam} \
VALIDATION_STRINGENCY=SILENT \
REFERENCE_SEQUENCE=${ref_fasta} \
INCLUDE_BQ_HISTOGRAM=true \
INTERVALS=${wgs_coverage_interval_list} \
OUTPUT=${metrics_filename} \
USE_FAST_ALGORITHM=true \
READ_LENGTH=${read_length}
}
runtime {
preemptible: preemptible_tries
memory: "3 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
}
output {
File metrics = "${metrics_filename}"
}
}
# Generate a checksum per readgroup
task CalculateReadGroupChecksum {
File input_bam
File input_bam_index
String read_group_md5_filename
Float disk_size
Int preemptible_tries
command {
java -Xms1000m -jar /usr/gitc/picard.jar \
CalculateReadGroupChecksum \
INPUT=${input_bam} \
OUTPUT=${read_group_md5_filename}
}
runtime {
preemptible: preemptible_tries
memory: "2 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
}
output {
File md5_file = "${read_group_md5_filename}"
}
}
# Notes on the contamination estimate:
# The contamination value is read from the FREEMIX field of the selfSM file output by verifyBamId
#
# In Zamboni production, this value is stored directly in METRICS.AGGREGATION_CONTAM
#
# Contamination is also stored in GVCF_CALLING and thereby passed to HAPLOTYPE_CALLER
# But first, it is divided by an underestimation factor thusly:
# float(FREEMIX) / ContaminationUnderestimationFactor
# where the denominator is hardcoded in Zamboni:
# val ContaminationUnderestimationFactor = 0.75f
#
# Here, I am handling this by returning both the original selfSM file for reporting, and the adjusted
# contamination estimate for use in variant calling
task CheckContamination {
File input_bam
File input_bam_index
File contamination_sites_ud
File contamination_sites_bed
File contamination_sites_mu
File ref_fasta
File ref_fasta_index
String output_prefix
Float disk_size
Int preemptible_tries
Float contamination_underestimation_factor
command <<<
set -e
# creates a ${output_prefix}.selfSM file, a TSV file with 2 rows, 19 columns.
# First row are the keys (e.g., SEQ_SM, RG, FREEMIX), second row are the associated values
/usr/gitc/VerifyBamID \
--Verbose \
--NumPC 4 \
--Output ${output_prefix} \
--BamFile ${input_bam} \
--Reference ${ref_fasta} \
--UDPath ${contamination_sites_ud} \
--MeanPath ${contamination_sites_mu} \
--BedPath ${contamination_sites_bed} \
1>/dev/null
# used to read from the selfSM file and calculate contamination, which gets printed out
python3 <<CODE
import csv
import sys
with open('${output_prefix}.selfSM') as selfSM:
reader = csv.DictReader(selfSM, delimiter='\t')
i = 0
for row in reader:
if float(row["FREELK0"])==0 and float(row["FREELK1"])==0:
# a zero value for the likelihoods implies no data. This usually indicates a problem rather than a real event.
# if the bam isn't really empty, this is probably due to the use of a incompatible reference build between
# vcf and bam.
sys.stderr.write("Found zero likelihoods. Bam is either very-very shallow, or aligned to the wrong reference (relative to the vcf).")
sys.exit(1)
print(float(row["FREEMIX"])/${contamination_underestimation_factor})
i = i + 1
# there should be exactly one row, and if this isn't the case the format of the output is unexpectedly different
# and the results are not reliable.
if i != 1:
sys.stderr.write("Found %d rows in .selfSM file. Was expecting exactly 1. This is an error"%(i))
sys.exit(2)
CODE
>>>
runtime {
preemptible: preemptible_tries
memory: "2 GB"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
docker: "broadinstitute/verify-bam-id:c8a66425c312e5f8be46ab0c41f8d7a1942b6e16-1500298351"
}
output {
File selfSM = "${output_prefix}.selfSM"
Float contamination = read_float(stdout())
}
}
# Convert BAM file to CRAM format
# Note that reading CRAMs directly with Picard is not yet supported
task ConvertToCram {
File input_bam
File ref_fasta
File ref_fasta_index
String output_basename
Float disk_size
Int preemptible_tries
command <<<
set -e
set -o pipefail
samtools view -C -T ${ref_fasta} ${input_bam} | \
tee ${output_basename}.cram | \
md5sum | awk '{print $1}' > ${output_basename}.cram.md5
# Create REF_CACHE. Used when indexing a CRAM
seq_cache_populate.pl -root ./ref/cache ${ref_fasta}
export REF_PATH=:
export REF_CACHE=./ref/cache/%2s/%2s/%s
samtools index ${output_basename}.cram
>>>
runtime {
preemptible: preemptible_tries
memory: "3 GB"
cpu: "1"
disks: "local-disk " + sub(disk_size, "\\..*", "") + " HDD"
}
output {
File output_cram = "${output_basename}.cram"
File output_cram_index = "${output_basename}.cram.crai"
File output_cram_md5 = "${output_basename}.cram.md5"
}
}