-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxenoclassify.wdl
executable file
·460 lines (402 loc) · 14.4 KB
/
xenoclassify.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
version 1.0
# imports workflows for the top portion of WGSPipeline
import "imports/pull_bwamem2.wdl" as bwaMem
import "imports/pull_star.wdl" as star
struct InputGroup {
File fastqR1
File fastqR2
String readGroup
}
workflow xenoClassify {
input {
Array[InputGroup] inputs
String reference
String hostReference = "mm10"
String libraryDesign
String outputFileNamePrefix = ""
Boolean filterSupAlignments = true
}
String outputPrefix = outputFileNamePrefix
# We fully support only single-lane data for WG. Workflow won't fail with multiple lanes but will return merged bam and bai
# + merged report
if (libraryDesign == "WG" || libraryDesign == "EX" || libraryDesign == "TS") {
scatter(inp in inputs) {
call bwaMem.bwamem2 as generateHostBamWG {
input:
fastqR1 = inp.fastqR1,
fastqR2 = inp.fastqR2,
runBwamem2_readGroups = inp.readGroup,
reference = hostReference,
outputFileNamePrefix = "host"
}
call bwaMem.bwamem2 as generateGraftBamWG {
input:
fastqR1 = inp.fastqR1,
fastqR2 = inp.fastqR2,
runBwamem2_readGroups = inp.readGroup,
reference = reference,
outputFileNamePrefix = "graft"
}
call sortBam as sortHostBamWG { input: inBam = generateHostBamWG.bwamem2Bam }
call sortBam as sortGraftBamWG { input: inBam = generateGraftBamWG.bwamem2Bam }
call classify as classifyWG { input: hostBam = sortHostBamWG.sortedBam, graftBam = sortGraftBamWG.sortedBam, outputPrefix = outputFileNamePrefix }
call filterHost as filterHostWG { input: xenoClassifyBam = classifyWG.xenoClassifyBam, rG = inp.readGroup, outputPrefix = outputFileNamePrefix }
}
call mergeBams { input: inputBams = filterHostWG.outputBam, outputPrefix = outputFileNamePrefix }
call mergeReports as mergeReportsWG { input: inputReports = classifyWG.jsonReport, inputRgs = filterHostWG.readGroup, outputPrefix = outputFileNamePrefix }
}
if (libraryDesign == "WT" || libraryDesign == "MR") {
scatter(inp in inputs) {
call star.star as generateHostBamWT {
input:
inputGroups = [inp],
reference = hostReference,
outputFileNamePrefix = "host"
}
call sortBam as sortHostBamWT { input: inBam = generateHostBamWT.starBam, filterSupAlignments = filterSupAlignments }
call star.star as generateGraftBamWT {
input:
inputGroups = [inp],
reference = reference,
outputFileNamePrefix = "graft"
}
call sortBam as sortGraftBamWT { input: inBam = generateGraftBamWT.starBam, filterSupAlignments = filterSupAlignments }
call classify as classifyWT { input: hostBam = sortHostBamWT.sortedBam, graftBam = sortGraftBamWT.sortedBam, outputPrefix = outputFileNamePrefix }
call filterHost as filterHostWT { input: xenoClassifyBam = classifyWT.xenoClassifyBam, rG = inp.readGroup, outputPrefix = outputFileNamePrefix }
call makeFastq { input: inputBam = filterHostWT.outputBam, rG = inp.readGroup, outputPrefix = outputFileNamePrefix }
}
# Also, re-align filtered data with star and merge reports from the classify task
call star.star as generateFinalBamWT {
input:
inputGroups = makeFastq.fastqData,
reference = reference,
outputFileNamePrefix = outputFileNamePrefix
}
call mergeReports as mergeReportsWT { input: inputReports = classifyWT.jsonReport, inputRgs = makeFastq.readGroup, outputPrefix = outputFileNamePrefix }
}
output {
File filteredResults = select_first([generateFinalBamWT.starBam, mergeBams.outputBam])
File filteredResultsIndex = select_first([generateFinalBamWT.starIndex, mergeBams.outputIndex])
File? starChimeric = generateFinalBamWT.starChimeric
File? transcriptomeBam = generateFinalBamWT.transcriptomeBam
File? geneReadFile = generateFinalBamWT.geneReadFile
File jsonReport = select_first([mergeReportsWT.jsonReport, mergeReportsWG.jsonReport])
}
parameter_meta {
inputs: "Array of fastq files for read 1 and 2 along with rG string"
libraryDesign: "Supported library design acronym. We support WG, EX, TS, WT and MR. Default is WG"
reference: "The reference of Graft to align the data with by either STAR or BWA"
hostReference: "The reference for host, most of the time it is mouse mm10"
filterSupAlignments: "Remove supplemental alignments from WT data (default true)"
outputFileNamePrefix: "Output file name prefix"
}
meta {
author: "Peter Ruzanov"
email: "[email protected]"
description: "Xenoclassify workflow classifies short-read sequencing data generated from xenograft samples. It requires alignment to the reference genomes of the graft and host species using bwamem2 or STAR. Once aligned, reads (or read pairs) are assessed to identify the likely source of the cells from which the DNA/RNA was extracted. The output is a bam file with reads tagged to indicate the source species. Also, the workflow creates a report in JSON format. The workflow uses [XenoClassify](https://github.com/oicr-gsi/xenoclassify).\n\n ![Xenoclassify, how it works](docs/xenoclassify_wf.png)\n"
dependencies: [
{
name: "samtools/1.14",
url: "https://github.com/samtools/samtools/archive/1.14.tar.gz"
},
{
name: "xenoclassify/1.0",
url: "https://github.com/oicr-gsi/xenoclassify/archive/1.1.tar.gz"
}
]
output_meta: {
filteredResults: {
description: "bam file without host (most commonly mouse) reads",
vidarr_label: "filteredResults"
},
filteredResultsIndex: {
description: "index file for file without host reads",
vidarr_label: "filteredResultsIndex"
},
starChimeric: {
description: "Chimeric Graft junctions, provisioned for WT data only",
vidarr_label: "starChimeric"
},
transcriptomeBam: {
description: "transcriptomeBam is a file produced for Graft WT data only",
vidarr_label: "transcriptomeBam"
},
geneReadFile: {
description: ".tab file with Graft gene read outs, only for WT data",
vidarr_label: "geneReadFile"
},
jsonReport: {
description: "a simple stats file with counts for differently tagged reads",
vidarr_label: "jsonReport"
}
}
}
}
# =============================================
# run BWAmem as a subworkflow, and then
# TASK 1 of 3: sort bam
# =============================================
task sortBam {
input {
File inBam
Int jobMemory = 10
String? tmpDir
String modules = "samtools/1.14"
Boolean filterSupAlignments = true
Int timeout = 72
}
command <<<
samtools sort -n ~{inBam} ~{'-T ' + tmpDir} ~{if (filterSupAlignments) then " | samtools view -F 2048 - -bh > " else "-o "} ~{basename(inBam, '.bam')}_sorted.bam
>>>
parameter_meta {
inBam: "Input .bam file"
tmpDir: "Optionally supply tmpDir for writing chunk bam files for sorting"
jobMemory: "Memory allocated to sort task"
modules: "Names and versions of modules needed for sorting"
filterSupAlignments: "Optional flag for removing supplemental (chimeric) alignments to prevent failures with WT data"
timeout: "Timeout for this task in hours"
}
runtime {
memory: "~{jobMemory} GB"
modules: "~{modules}"
timeout: "~{timeout}"
}
output {
File sortedBam = "~{basename(inBam, '.bam')}_sorted.bam"
}
}
# ===================================
# TASK 2 of 3: run xenoclassify bam
# ===================================
task classify {
input {
File hostBam
File graftBam
String outputPrefix
String modules = "samtools/1.14 xenoclassify/1.0"
Int jobMemory = 10
Int neitherThreshold = 20
Int tolerance = 5
Int difference = 5
Int timeout = 72
}
command <<<
set -euo pipefail
python3 $XENOCLASSIFY_ROOT/bin/xenoclassify/xenoclassify.py -H ~{hostBam} -G ~{graftBam} -O . -b -p ~{outputPrefix} \
-n ~{neitherThreshold} -t ~{tolerance} -d ~{difference}
python3<<CODE
import json
import os
import re
json_name = "~{outputPrefix}_tagReport.json"
jsonDict = {}
command = "samtools view ~{outputPrefix}_output.bam | awk \'{print \$NF}\' | sort | uniq -c"
counts = os.popen(command).read().splitlines()
for line in counts:
if line.find('CL:Z:') == 0:
continue
line = line.rstrip()
line = re.sub('CL:Z:', '', line)
tmp = line.split()
jsonDict[tmp[1]] = tmp[0]
with open(json_name, 'w') as json_file:
json.dump(jsonDict, json_file)
CODE
>>>
parameter_meta {
hostBam: "Input host .bam file"
graftBam: "Input graft .bam file"
outputPrefix: "Output prefix"
neitherThreshold: "Threshold for score below which the reads are classified as 'neither'"
tolerance: "Tolerance around the mean of alignment scores for a set of reads classified as 'both'"
difference: "Difference between the sum of host and graft alignment scores for a set of reads classified as 'both'"
jobMemory: "Memory allocated to classify task"
timeout: "Timeout for this task in hours"
modules: "Names and versions of modules needed for classification"
}
runtime {
memory: "~{jobMemory} GB"
modules: "~{modules}"
timeout: "~{timeout}"
}
output {
File xenoClassifyBam = "~{outputPrefix}_output.bam"
File jsonReport = "~{outputPrefix}_tagReport.json"
}
}
# ================================
# TASK 3 of 3: filter bam
# ================================
task filterHost {
input {
File xenoClassifyBam
String outputPrefix = "OUTPUT"
String modules = "samtools/1.14"
String? tmpDir
String rG
Array[String] filterTags = ["host"]
Int jobMemory = 5
Int timeout = 72
}
parameter_meta {
xenoClassifyBam: "Classified .bam file"
tmpDir: "Optionally supply tmpDir for writing chunk bam files for sorting"
outputPrefix: "Prefix for making filtered bam name"
modules: "Names and versions of modules needed for filtering"
filterTags: "Filter reads with these tags"
rG: "Read group string for passing to the downstream process"
jobMemory: "Memory allocated to filtering task"
timeout: "Timeout for this task in hours"
}
command <<<
set -euo pipefail
python3<<CODE
import os
inputTags = "~{sep=' ' filterTags}"
tags = inputTags.split()
command = "samtools view -h ~{xenoClassifyBam}"
for t in tags:
command = command + " | grep -v \'CL:Z:" + t + "\'"
command = command + " | samtools sort -O bam ~{'-T ' + tmpDir} -o ~{outputPrefix}_filtered.bam -"
os.system(command)
CODE
samtools index ~{outputPrefix}_filtered.bam ~{outputPrefix}_filtered.bai
>>>
runtime {
memory: "~{jobMemory} GB"
modules: "~{modules}"
timeout: "~{timeout}"
}
output {
File outputBam = "${outputPrefix}_filtered.bam"
File outputBai = "${outputPrefix}_filtered.bai"
String readGroup = "~{rG}"
}
}
# ====================================
# Optional for WT: Make fastq files
# ====================================
task makeFastq {
input {
Int jobMemory = 24
Int overhead = 6
Int timeout = 20
File inputBam
String outputPrefix
String rG
String picardParams = "VALIDATION_STRINGENCY=LENIENT"
String modules = "samtools/1.14 picard/2.21.2"
}
Int javaMemory = jobMemory - overhead
command <<<
set -euo pipefail
unset _JAVA_OPTIONS
java -Xmx~{javaMemory}G -jar $PICARD_ROOT/picard.jar SamToFastq I=~{inputBam} F=FILTERED_1.fastq F2=FILTERED_2.fastq ~{picardParams}
gzip -c FILTERED_1.fastq > ~{outputPrefix}_part_1.fastq.gz
gzip -c FILTERED_2.fastq > ~{outputPrefix}_part_2.fastq.gz
>>>
parameter_meta {
inputBam: "Input bam file, BWA-aligned reads"
outputPrefix: "Output prefix for the result file"
jobMemory: "Memory allocated to the task."
rG: "Read group string for passing to the downstream process"
overhead: "Ovrerhead for calculating heap memory, difference between total and Java-allocated memory"
picardParams: "Additional parameters for picard SamToFastq, Default is VALIDATION_STRINGENCY=LENIENT"
modules: "Names and versions of required modules."
timeout: "Timeout in hours, needed to override imposed limits."
}
runtime {
memory: "~{jobMemory} GB"
modules: "~{modules}"
timeout: "~{timeout}"
}
output {
InputGroup fastqData = {"fastqR1":"~{outputPrefix}_part_1.fastq.gz", "fastqR2":"~{outputPrefix}_part_2.fastq.gz", "readGroup":"~{rG}"}
String readGroup = "~{rG}"
}
}
# =========================================
# Optional for WT: Merge classify reports
# =========================================
task mergeReports {
input {
Array[File] inputReports
Array[String] inputRgs
String outputPrefix
String modules = ""
Int jobMemory = 4
Int timeout = 4
}
parameter_meta {
inputReports: "Array of input JSON files"
inputRgs: "Array of RG strings"
outputPrefix: "Output prefix for the result file"
jobMemory: "Memory for the task, in gigabytes"
modules: "Environment modules for the task"
timeout: "Timeout for the task, in hours"
}
command <<<
python <<CODE
import json
import re
r = "~{sep=' ' inputReports}"
inputJsons = r.split()
inputRgs = "~{sep=' ' inputRgs}"
data = {}
def jsonRead(fileName):
with open(fileName, "r") as f:
jsonText = f.readlines()
jsonText = "".join(jsonText)
jsonText = jsonText.strip()
return json.loads(jsonText)
matches = re.findall('(?<=[ID]:)([\S]*)', inputRgs)
for j in range(len(inputJsons)):
if matches[j]:
data[matches[j]] = jsonRead(inputJsons[j])
metrics_file = "~{outputPrefix}_tagReport.json"
with open(metrics_file, "w") as m:
m.write(json.dumps(data, indent=2))
CODE
>>>
runtime {
memory: "~{jobMemory} GB"
modules: "~{modules}"
timeout: "~{timeout}"
}
output {
File jsonReport = "~{outputPrefix}_tagReport.json"
}
}
# =========================================
# WG: Merge filtered bam files
# =========================================
task mergeBams {
input {
Array[File] inputBams
String outputPrefix
String modules = "samtools/1.14"
Int jobMemory = 8
Int timeout = 8
}
parameter_meta {
inputBams: "Array of input BAM files"
outputPrefix: "Output prefix for the result file"
jobMemory: "Memory for the task, in gigabytes"
modules: "Environment modules for the task"
timeout: "Timeout for the task, in hours"
}
command <<<
set -euo pipefail
samtools merge -o ~{outputPrefix}_filtered.bam ~{sep=" " inputBams}
samtools index ~{outputPrefix}_filtered.bam ~{outputPrefix}_filtered.bai
>>>
runtime {
memory: "~{jobMemory} GB"
modules: "~{modules}"
timeout: "~{timeout}"
}
output {
File outputBam = "~{outputPrefix}_filtered.bam"
File outputIndex = "~{outputPrefix}_filtered.bai"
}
}