-
Notifications
You must be signed in to change notification settings - Fork 0
/
cram-to-bam.wdl
executable file
·160 lines (141 loc) · 5 KB
/
cram-to-bam.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
## Copyright Broad Institute, 2017
## This script should convert a CRAM to SAM to BAM and output a BAM, BAM Index, and validation report to a Google bucket. If you'd like to do ## this on multiple CRAMS, create a sample set in the Data tab.
## The reason this approach was chosen instead of converting CRAM to BAM directly using Samtools is because Samtools 1.3 produces incorrect
## bins due to an old version of htslib included in the package. Samtools versions 1.4 & 1.5 have an NM issue that causes them to not validate ## with Picard.
##
## TESTED: It was tested using the Genomes in the Cloud Docker image version 2.3.1-1500064817.
## Versions of other tools on this image at the time of testing:
## PICARD_VER=1.1150
## GATK34_VER=3.4-g3c929b0
## GATK35_VER=3.5-0-g36282e4
## GATK36_VER=3.6-44-ge7d1cd2
## GATK4_VER=4.beta.1
## SAMTOOLS_VER=1.3.1
## BWA_VER=0.7.15.r1140
## TABIX_VER=0.2.5_r1005
## BGZIP_VER=1.3
## SVTOOLKIT_VER=2.00-1650
## It was tested pulling the HG38 reference Fasta and Fai.
## Successfully tested on Cromwell version 28. Does not work on versions < v23 due to output syntax
## Runtime parameters are optimized for Broad's Google Cloud Platform implementation.
##
## 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 for detailed licensing information pertaining to the included programs.
##
#WORKFLOW DEFINITION
workflow CramToBamFlow {
Int cram_to_bam_disk_size
Int validate_sam_file_disk_size
String cram_to_bam_mem_size
String validate_sam_file_mem_size
String sample_name
String? gotc_docker_override
String gotc_docker = select_first([gotc_docker_override, "broadinstitute/genomes-in-the-cloud:2.3.1-1500064817"])
File RefFasta
File RefIndex
File RefDict
File InputCram
#converts CRAM to SAM to BAM and makes BAI
call CramToBamTask{
input:
disk_size = cram_to_bam_disk_size,
mem_size = cram_to_bam_mem_size,
docker_image = gotc_docker,
SampleName = sample_name,
RefFasta = RefFasta,
RefIndex = RefIndex,
RefDict = RefDict,
InputCram = InputCram
}
#validates Bam
call ValidateSamFile{
input:
input_bam = CramToBamTask.outputBam,
disk_size = validate_sam_file_disk_size,
mem_size = validate_sam_file_mem_size,
docker_image = gotc_docker
}
#Outputs Bam, Bai, and validation report to the FireCloud data model
output {
File outputBam = CramToBamTask.outputBam
File outputBai = CramToBamTask.outputBai
File validation_report = ValidateSamFile.report
}
}
#Task Definitions
task CramToBamTask {
File RefFasta
File RefIndex
File RefDict
File InputCram
#File InputCrai
String SampleName
Int disk_size
String mem_size
String docker_image
#Calls samtools view to do the conversion
command {
#Set -e and -o says if any command I run fails in this script, make sure to return a failure
#Changed to check that the input is a Cram, otherwise if it is a Bam, it does not do a conversion,
#or if it is neither, it returns an error.
set -e
set -o pipefail
if [[ "`basename ${InputCram} | sed 's/.*\.//'`" = "cram" ]]; then
samtools view -@ 2 -h -T ${RefFasta} ${InputCram} | samtools view -@ 2 -b -o ${SampleName}.bam -
elif [[ "`basename ${InputCram} | sed 's/.*\.//'`" = "bam" ]]; then
mv ${InputCram} ${SampleName}.bam
else
exit 1
fi
samtools index ${SampleName}.bam
mv ${SampleName}.bam.bai ${SampleName}.bai
}
#Run time attributes:
#Use a docker with samtools. Set this up as a workspace attribute.
#cpu of one because no multi-threading is required. This is also default, so don't need to specify.
#disk_size should equal input size + output size + buffer
runtime {
#docker: docker_image
docker: "halllab/samtools:v1.9"
memory: mem_size
cpu: "4"
disks: "local-disk " + disk_size + " HDD"
}
#Outputs a BAM and BAI with the same sample name
output {
File outputBam = "${SampleName}.bam"
File outputBai = "${SampleName}.bai"
}
}
#Validates BAM output to ensure it wasn't corrupted during the file conversion
task ValidateSamFile {
File input_bam
String output_name = basename(input_bam, ".bam") + ".validation_report"
Int disk_size
String mem_size
Int preemptible_tries
String docker_image
command {
java -Xmx3000m -jar /usr/gitc/picard.jar \
ValidateSamFile \
INPUT=${input_bam} \
OUTPUT=${output_name} \
MODE=SUMMARY \
IS_BISULFITE_SEQUENCED=false
}
#Run time attributes:
#Use a docker with the picard.jar. Set this up as a workspace attribute.
#Read more about return codes here: https://github.com/broadinstitute/cromwell#continueonreturncode
runtime {
docker: docker_image
memory: mem_size
disks: "local-disk " + disk_size + " HDD"
preemptible: preemptible_tries
continueOnReturnCode: [0,1]
}
#A text file is generated that will list errors or warnings that apply.
output {
File report = "${output_name}"
}
}