-
Notifications
You must be signed in to change notification settings - Fork 4
/
PairedSingleSampleWf_noqc_nocram_optimized.wdl
executable file
·1716 lines (1570 loc) · 58.9 KB
/
PairedSingleSampleWf_noqc_nocram_optimized.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.
# TASK DEFINITIONS
# Collect sequencing yield quality metrics
task CollectQualityYieldMetrics {
File input_bam
String metrics_filename
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command {
java -Xmx${java_heap_memory_initial} -jar ${tool_path}/picard.jar \
CollectQualityYieldMetrics \
INPUT=${input_bam} \
OQ=true \
OUTPUT=${metrics_filename}
}
runtime {
memory: memory
cpu: cpu
# backend: "Local"
}
output {
File metrics = "${metrics_filename}"
}
}
# Check the assumption that the final GVCF filename that is going to be used ends with .g.vcf.gz
task CheckFinalVcfExtension {
String vcf_filename
String memory
Int cpu
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 {
memory: memory
cpu: cpu
# backend: "Local"
}
output {
String common_suffix=read_string(stdout())
}
}
# Get version of BWA
task GetBwaVersion {
String memory
Int cpu
String tool_path
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.
${tool_path}/bwa/bwa 2>&1 | \
grep -e '^Version' | \
sed 's/Version: //'
}
runtime {
memory: memory
cpu: cpu
# backend: "Local"
}
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
String tool_path
# 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
Int compression_level
String java_heap_memory_initial
String memory
Int cpu
Int bwa_threads
command <<<
set -o pipefail
set -e
# set the bash variable needed for the command-line
bash_ref_fasta=${ref_fasta}
bwa_threads=${bwa_threads}
# if ref_alt has data in it,
if [ -s ${ref_alt} ]; then
java -Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial} -jar ${tool_path}/picard.jar \
SamToFastq \
INPUT=${input_bam} \
FASTQ=/dev/stdout \
INTERLEAVE=true \
NON_PF=true | \
${tool_path}/${bwa_commandline} /dev/stdin - 2> >(tee ${output_bam_basename}.bwa.stderr.log >&2) | \
java -Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial} -jar ${tool_path}/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 {
memory: memory
cpu: cpu
# backend: "SLURM-BWA"
}
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 compression_level
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command {
java -Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial} -jar ${tool_path}/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=2700000
}
runtime {
cpu: cpu
memory: memory
}
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"
}
}
# Sort BAM file by coordinate order and fix tag values for NM and UQ
task SamtoolsSort {
File input_bam
String output_bam_basename
Int compression_level
String java_heap_memory_initial
String memory
Int cpu
Int samtools_threads
String mem_limit
String tool_path
command <<<
set -o pipefail
set -e
${tool_path}/samtools-1.9/samtools sort -T $TMPDIR/${output_bam_basename}.bam.tmp -m ${mem_limit} --threads ${samtools_threads} -l ${compression_level} ${input_bam} -o ${output_bam_basename}.bam
${tool_path}/samtools-1.9/samtools index ${output_bam_basename}.bam ${output_bam_basename}.bai
>>>
runtime {
cpu: cpu
memory: memory
}
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
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command {
java -Xmx${java_heap_memory_initial} -jar ${tool_path}/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: memory
cpu: cpu
}
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
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command {
java -Xmx${java_heap_memory_initial} -jar ${tool_path}/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: memory
cpu: cpu
}
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
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command {
java -Xmx${java_heap_memory_initial} -jar ${tool_path}/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: memory
cpu: cpu
}
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
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command <<<
java -Dsamjdk.buffer_size=131072 \
-XX:GCTimeLimit=50 -XX:GCHeapFreeLimit=10 -Xmx${java_heap_memory_initial} \
-jar ${tool_path}/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 {
memory: memory
cpu: cpu
}
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
String sample
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command <<<
java -Dsamjdk.buffer_size=131072 \
-XX:GCTimeLimit=50 -XX:GCHeapFreeLimit=10 -Xmx${java_heap_memory_initial} \
-jar ${tool_path}/picard.jar \
CheckFingerprint \
INPUT=${input_bam} \
OUTPUT=${output_basename} \
GENOTYPES=${genotypes} \
HAPLOTYPE_MAP=${haplotype_database_file} \
SAMPLE_ALIAS="${sample}" \
IGNORE_READ_GROUPS=true
>>>
runtime {
memory: memory
cpu: cpu
}
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
Int compression_level
String java_heap_memory_initial
String memory
Int cpu
String tool_path
# 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} -Xmx${java_heap_memory_initial} -jar ${tool_path}/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 {
memory: memory
cpu: cpu
}
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
String memory
Int cpu
# 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 {
memory: memory
cpu: cpu
}
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_bam_index
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
Int compression_level
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command <<<
export GATK_LOCAL_JAR=${tool_path}/gatk-4.1.4.0/gatk-package-4.1.4.0-local.jar && \
${tool_path}/gatk-4.1.4.0/gatk --java-options "-XX:GCTimeLimit=50 -XX:GCHeapFreeLimit=10 -XX:+PrintFlagsFinal \
-XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+PrintGCDetails \
-Xloggc:gc_log.log -Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial}" \
BaseRecalibrator \
-R ${ref_fasta} \
-I ${input_bam} \
--use-original-qualities \
-O ${recalibration_report_filename} \
--known-sites ${dbSNP_vcf} \
--known-sites ${sep=" --known-sites " known_indels_sites_VCFs} \
-L ${sep=" -L " sequence_group_interval}
>>>
runtime {
memory: memory
cpu: cpu
}
output {
File recalibration_report = "${recalibration_report_filename}"
}
}
# Apply Base Quality Score Recalibration (BQSR) model
task ApplyBQSR {
File input_bam
File input_bam_index
String output_bam_basename
File recalibration_report
Array[String] sequence_group_interval
File ref_dict
File ref_fasta
File ref_fasta_index
Int compression_level
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command <<<
export GATK_LOCAL_JAR=${tool_path}/gatk-4.1.4.0/gatk-package-4.1.4.0-local.jar && \
${tool_path}/gatk-4.1.4.0/gatk --java-options "-XX:+PrintFlagsFinal -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps \
-XX:+PrintGCDetails -Xloggc:gc_log.log \
-XX:GCTimeLimit=50 -XX:GCHeapFreeLimit=10 -Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial}" \
ApplyBQSR \
--create-output-bam-md5 \
--add-output-sam-program-record \
-R ${ref_fasta} \
-I ${input_bam} \
--use-original-qualities \
-O ${output_bam_basename}.bam \
-bqsr ${recalibration_report} \
--static-quantized-quals 10 --static-quantized-quals 20 --static-quantized-quals 30 \
-L ${sep=" -L " sequence_group_interval}
>>>
runtime {
memory: memory
cpu: cpu
}
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 compression_level
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command <<<
export GATK_LOCAL_JAR=${tool_path}/gatk-4.1.4.0/gatk-package-4.1.4.0-local.jar && \
${tool_path}/gatk-4.1.4.0/gatk --java-options "-Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial}" \
GatherBQSRReports \
-I ${sep=' -I ' input_bqsr_reports} \
-O ${output_report_filename}
>>>
runtime {
memory: memory
cpu: cpu
}
output {
File output_bqsr_report = "${output_report_filename}"
}
}
# Combine multiple recalibrated BAM files from scattered ApplyRecalibration runs
task GatherBamFiles {
Array[File] input_bams
String output_bam_basename
Int compression_level
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command {
java -Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial} -jar ${tool_path}/picard.jar \
GatherBamFiles \
INPUT=${sep=' INPUT=' input_bams} \
OUTPUT=${output_bam_basename}.bam \
CREATE_INDEX=true \
CREATE_MD5_FILE=true
}
runtime {
memory: memory
cpu: cpu
}
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
Int compression_level
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command {
java -Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial} -jar ${tool_path}/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 {
memory: memory
cpu: cpu
}
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 = 250
Int compression_level
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command {
java -Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial} -jar ${tool_path}/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 {
memory: memory
cpu: cpu
}
output {
File metrics = "${metrics_filename}"
}
}
# Collect raw WGS metrics (commonly used QC thresholds)
task CollectRawWgsMetrics {
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 = 250
Int compression_level
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command {
java -Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial} -jar ${tool_path}/picard.jar \
CollectRawWgsMetrics \
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 {
memory: memory
cpu: cpu
}
output {
File metrics = "${metrics_filename}"
}
}
# Generate a checksum per readgroup
task CalculateReadGroupChecksum {
File input_bam
File input_bam_index
String read_group_md5_filename
Int compression_level
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command {
java -Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial} -jar ${tool_path}/picard.jar \
CalculateReadGroupChecksum \
INPUT=${input_bam} \
OUTPUT=${read_group_md5_filename}
}
runtime {
memory: memory
cpu: cpu
}
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 contamination_underestimation_factor
String memory
Int cpu
String tool_path
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 \
${tool_path}/VerifyBamID/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 {
memory: memory
cpu: cpu
}
output {
File selfSM = "${output_prefix}.selfSM"
Float contamination = read_float(stdout())
}
}
# This task calls picard's IntervalListTools to scatter the input interval list into scatter_count sub interval lists
# Note that the number of sub interval lists may not be exactly equal to scatter_count. There may be slightly more or less.
# Thus we have the block of python to count the number of generated sub interval lists.
task ScatterIntervalList {
File interval_list
Int scatter_count
Int break_bands_at_multiples_of
Int compression_level
String java_heap_memory_initial
String memory
Int cpu
String tool_path
command <<<
set -e
mkdir out
java -Dsamjdk.compression_level=${compression_level} -Xmx${java_heap_memory_initial} -jar ${tool_path}/picard.jar \
IntervalListTools \
SCATTER_COUNT=${scatter_count} \
SUBDIVISION_MODE=BALANCING_WITHOUT_INTERVAL_SUBDIVISION_WITH_OVERFLOW \
UNIQUE=true \
SORT=true \
BREAK_BANDS_AT_MULTIPLES_OF=${break_bands_at_multiples_of} \
INPUT=${interval_list} \
OUTPUT=out
python3 <<CODE
import glob, os
# Works around a JES limitation where multiples files with the same name overwrite each other when globbed
intervals = sorted(glob.glob("out/*/*.interval_list"))
for i, interval in enumerate(intervals):
(directory, filename) = os.path.split(interval)
newName = os.path.join(directory, str(i + 1) + filename)
os.rename(interval, newName)
print(len(intervals))
CODE
>>>
output {
Array[File] out = glob("out/*/*.interval_list")
Int interval_count = read_int(stdout())
}
runtime {
memory: memory
cpu: cpu
# backend: "Local"
}
}
# Call variants on a single sample with HaplotypeCaller to produce a GVCF
task HaplotypeCaller {
File input_bam
File input_bam_index
File interval_list
String gvcf_basename
File ref_dict
File ref_fasta
File ref_fasta_index
Float? contamination
String gatk_gkl_pairhmm_implementation
Int gatk_gkl_pairhmm_threads
Int compression_level
String haplotypecaller_java_heap_memory_initial
String memory
Int cpu
String tool_path
String smith_waterman_implementation
# We use interval_padding 500 below to make sure that the HaplotypeCaller has context on both sides around
# the interval because the assembly uses them.
command <<<
export GATK_LOCAL_JAR=${tool_path}/gatk-4.1.4.0/gatk-package-4.1.4.0-local.jar && \
${tool_path}/gatk-4.1.4.0/gatk --java-options -Xmx${haplotypecaller_java_heap_memory_initial} \
HaplotypeCaller \
-R ${ref_fasta} \
-I ${input_bam} \
-O ${gvcf_basename}.vcf.gz \
-L ${interval_list} \
-ip 100 \
-contamination ${default=0 contamination} \
--max-alternate-alleles 3 \
-ERC GVCF \
--pair-hmm-implementation ${gatk_gkl_pairhmm_implementation} \
--native-pair-hmm-threads ${gatk_gkl_pairhmm_threads} \
--smith-waterman ${smith_waterman_implementation}
>>>
runtime {
memory: memory
cpu: cpu
# backend: "SLURM-HAPLO"
}
output {
File output_gvcf = "${gvcf_basename}.vcf.gz"
File output_gvcf_index = "${gvcf_basename}.vcf.gz.tbi"
}
}
# Combine multiple VCFs or GVCFs from scattered HaplotypeCaller runs
task MergeVCFs {
Array[File] input_vcfs