-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.nf
470 lines (428 loc) · 14.8 KB
/
main.nf
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
#!/usr/bin/env nextflow
// Developer notes
//
// This template workflow provides a basic structure to copy in order
// to create a new workflow. Current recommended pratices are:
// i) create a simple command-line interface.
// ii) include an abstract workflow scope named "pipeline" to be used
// in a module fashion
// iii) a second concreate, but anonymous, workflow scope to be used
// as an entry point when using this workflow in isolation.
import groovy.json.JsonBuilder
nextflow.enable.dsl = 2
include { fastq_ingress } from './lib/ingress'
include { xam_ingress } from './lib/ingress'
include {
nextclade as nextclade_ref;
nextclade as nextclade_denovo
} from './modules/local/nextclade'
OPTIONAL_FILE = file("$projectDir/data/OPTIONAL_FILE")
process alignReads {
label "wfmpx"
cpus 4
memory '2GB'
input:
// `reads` can be FASTQ, BAM, or uBAM
tuple val(meta), path("input"), path("index")
path reference
output:
tuple val(sample_id), val(type), path("${sample_id}.bam"), path("${sample_id}.bam.bai"), emit: alignment
tuple path("${sample_id}.bamstats"), path("${sample_id}.bam.summary"), emit: bamstats
script:
sample_id = meta.alias
type = meta.type
"""
# if FASTQ or uBAM, align
if [[ ${params.fastq || meta.is_unaligned} = true ]]; then
${params.fastq ? "cat" : "samtools fastq -T '*'" } input \\
| minimap2 -ax map-ont --secondary=no -L --MD -t 3 "$reference" - \\
--cap-kalloc 100m --cap-sw-mem 50m \\
| samtools sort --write-index -o ${sample_id}.bam##idx##${sample_id}.bam.bai
else
# we got BAM; make sure it was aligned against the provided reference
samtools faidx "$reference"
ref_name=\$(cut -f1 ${reference}.fai)
bam_ref_name=\$(
samtools view -H input | grep '^@SQ' | grep -oP 'SN:\\K[^\\s]+'
)
if [[ \$bam_ref_name != \$ref_name ]]; then
>&2 echo -n "BAM file appears to have been aligned against a "
>&2 echo "different reference:"
>&2 echo "Ref. name in BAM file: '\$bam_ref_name'"
>&2 echo "Ref. name in '$reference': '\$ref_name'"
exit 1
fi
# no need to align, just rename
mv input ${sample_id}.bam
mv index ${sample_id}.bam.bai
fi
stats_from_bam -o ${sample_id}.bamstats -s ${sample_id}.bam.summary -t 4 ${sample_id}.bam
"""
}
process coverageCalc {
label "wfmpx"
cpus 1
memory '2GB'
input:
tuple val(sample_id), val(type), path("${sample_id}.bam"), path("${sample_id}.bam.bai")
path reference
output:
path "${sample_id}.depth.txt"
"""
coverage_from_bam -s 1 -p ${sample_id} ${sample_id}.bam
mv ${sample_id}*.depth.txt ${sample_id}.depth.txt
"""
}
process medakaVariants {
label "medaka"
cpus 1
memory '8GB'
input:
tuple val(sample_id), val(type), path("${sample_id}.bam"), path("${sample_id}.bam.bai"), val(basecall_model)
path reference
path genbank
output:
tuple val(sample_id), val(type), path("${sample_id}.annotate.filtered.vcf")
script:
"""
medaka consensus ${sample_id}.bam ${sample_id}.hdf --model ${basecall_model}:consensus
medaka variant --gvcf ${reference} ${sample_id}.hdf ${sample_id}.vcf --verbose
medaka tools annotate --debug --pad 25 ${sample_id}.vcf ${reference} ${sample_id}.bam ${sample_id}.annotate.vcf
bcftools filter -e "ALT='.'" ${sample_id}.annotate.vcf | bcftools filter -o ${sample_id}.annotate.filtered.vcf -O v -e "INFO/DP<${params.min_coverage}" -
# vcf-annotator ${sample_id}.annotate.filtered.vcf ${genbank} > ${sample_id}.vcf-annotator.vcf
"""
}
process makeConsensus {
label "wfmpx"
cpus 1
memory '500MB'
input:
tuple val(sample_id), val(type), path("${sample_id}.annotate.filtered.vcf")
path reference
path depth
output:
tuple val(sample_id), val(type), path("${sample_id}.ref.consensus.fasta")
"""
reference_name=`basename ${reference} .fasta`
awk -v ref=\${reference_name} '{if (\$2<${params.min_coverage}) print ref"\t"\$1+1}' ${depth} > mask.regions
bgzip ${sample_id}.annotate.filtered.vcf
tabix ${sample_id}.annotate.filtered.vcf.gz
bcftools consensus --mask mask.regions --mark-del '-' --mark-ins lc --fasta-ref ${reference} -o ${sample_id}.ref.consensus.fasta ${sample_id}.annotate.filtered.vcf.gz
"""
}
process flyeAssembly {
label "wfmpx"
cpus 4
memory '31GB'
input:
tuple val(sample_id), val(type), path("${sample_id}.bam"), path("${sample_id}.bam.bai")
path reference
output:
tuple val(sample_id), val(type), path("flye/assembly.fasta"), path("${sample_id}_restricted.fastq"), path(reference)
script:
"""
samtools bam2fq ${sample_id}.bam > ${sample_id}_restricted.fastq
flye --nano-raw ${sample_id}_restricted.fastq -g 197k -t 4 --meta -o flye
"""
}
process noAssembly{
label "wfmpx"
cpus 1
memory '10MB'
input:
tuple val(sample_id), val(type), path("${sample_id}.bam"), path("${sample_id}.bam.bai")
path reference
output:
tuple val(sample_id), val(type), path("medaka/denovo.consensus.fasta"), path("${sample_id}_assembly_mapped.bam"), path("${sample_id}_assembly.bed")
script:
"""
mkdir medaka
touch medaka/denovo.consensus.fasta
touch ${sample_id}_assembly_mapped.bam
touch ${sample_id}_assembly.bed
"""
}
process medaka_polish {
label "medaka"
cpus 2
memory '15GB'
input:
tuple val(sample_id),
val(type),
path("flye/assembly.fasta"),
path("${sample_id}_restricted.fastq"),
path(reference),
val(basecall_model)
output:
tuple val(sample_id), val(type), path("medaka/denovo.consensus.fasta"), path("${sample_id}_assembly_mapped.bam")
script:
"""
medaka_consensus -i ${sample_id}_restricted.fastq -t 1 -d flye/assembly.fasta \
-m ${basecall_model}:consensus
mv medaka/consensus.fasta medaka/denovo.consensus.fasta
minimap2 -ax map-ont ${reference} medaka/denovo.consensus.fasta -t 1 \\
--cap-kalloc 100m --cap-sw-mem 50m \\
| samtools sort -o ${sample_id}_assembly_mapped.bam
"""
}
process bamtobed {
label "wfmpx"
cpus 1
memory '500MB'
input:
tuple val(sample_id), val(type), path("medaka/denovo.consensus.fasta"), path("${sample_id}_assembly_mapped.bam")
output:
tuple val(sample_id), val(type), path("medaka/denovo.consensus.fasta"), path("${sample_id}_assembly_mapped.bam"), path("${sample_id}_assembly.bed")
script:
"""
bedtools bamtobed -i ${sample_id}_assembly_mapped.bam > ${sample_id}_assembly.bed
"""
}
process getVersions {
label "wfmpx"
cpus 1
memory '500MB'
output:
path "versions.txt"
script:
"""
python -c "import pysam; print(f'pysam,{pysam.__version__}')" >> versions.txt
fastcat --version | sed 's/^/fastcat,/' >> versions.txt
bedtools --version | sed 's/ /,/' >> versions.txt
flye --version | sed 's/^/flye,/' >> versions.txt
bcftools --version | head -1 | sed 's/ /,/' >> versions.txt
"""
}
process getVersions_medaka {
label "medaka"
cpus 1
memory '500MB'
input:
path "other_versions.txt"
output:
path "versions.txt"
script:
"""
cat other_versions.txt > versions.txt
medaka --version | sed 's/ /,/' >> versions.txt
"""
}
process getVersions_nextclade {
label "nextclade"
cpus 1
input:
path "other_versions.txt"
output:
path "versions.txt"
script:
"""
cat other_versions.txt > versions.txt
nextclade --version | sed 's/ /,/' >> versions.txt
"""
}
process getParams {
label "wfmpx"
cpus 1
memory '500MB'
output:
path "params.json"
script:
def paramsJSON = new JsonBuilder(params).toPrettyString()
"""
# Output nextflow params object to JSON
echo '$paramsJSON' > params.json
"""
}
process makeReport {
label "wfmpx"
cpus 1
memory '1GB'
input:
path "per-read-stats.tsv.gz"
path "versions/*"
path "params.json"
path variants
path coverage
path reference
tuple val(sample_id), val(type), path(consensus), path(mapped_consensus), path(assembly_bed)
path "nextclade_ref.json"
path nextclade_denovo_json, stageAs: "nextclade_denovo.json"
output:
path "wf-mpx-*.html"
script:
String report_name = "wf-mpx-report.html"
String nextclade_denovo_arg = params.assembly ? "--nextclade_denovo nextclade_denovo.json" : ""
"""
[ -e ${assembly_bed} ] || echo "" > ${assembly_bed};
workflow-glue report $report_name \
--versions versions \
per-read-stats.tsv.gz \
--params params.json \
--variants ${variants} \
--coverage ${coverage} \
--reference ${reference} \
--assembly_bed ${assembly_bed} \
--nextclade_ref nextclade_ref.json \
${nextclade_denovo_arg}
"""
}
// See https://github.com/nextflow-io/nextflow/issues/1636
// This is the only way to publish files from a workflow whilst
// decoupling the publish from the process steps.
process output {
// publish inputs to output directory
label "wfmpx"
cpus 1
memory '500MB'
publishDir (
"${params.out_dir}",
mode: 'copy',
saveAs: { fname_as ?: fname }
)
input:
tuple path(fname), val(fname_as)
output:
path fname
"""
echo "Writing output files."
"""
}
// workflow module
workflow pipeline {
take:
reads
reference
genbank
main:
// get basecall models: we use `params.override_basecaller_cfg` if present;
// otherwise use `meta.basecall_models[0]` (there should only be one value in
// the list because we're running ingress with `allow_multiple_basecall_models:
// false`; note that `[0]` on an empty list returns `null`)
basecall_models = reads
| map { meta, reads, index, stats ->
String basecall_model = \
params.override_basecaller_cfg ?: meta.basecall_models[0]
if (!basecall_model) {
error "Found no basecall model information in the input data for " + \
"sample '$meta.alias'. Please provide it with the " + \
"`--override_basecaller_cfg` parameter."
}
[meta.alias, basecall_model]
}
alignment = alignReads(
reads.map { meta, reads, index, stats -> [meta, reads, index] },
reference,
)
coverage = coverageCalc(alignment.alignment, reference)
variants = medakaVariants(
alignment.alignment.join(basecall_models),
reference,
genbank
)
consensus = makeConsensus(variants, reference, coverage)
nextclade_ref_json = nextclade_ref(consensus.map{it[2]}.collect(), "ref")
software_versions = getVersions()|getVersions_medaka|getVersions_nextclade
workflow_params = getParams()
if (params.assembly) {
assembly = flyeAssembly(alignment.alignment, reference)
| join(basecall_models)
| medaka_polish
| bamtobed
nextclade_denovo_json = nextclade_denovo(assembly.map{it[2]}.collect(), "denovo")
} else {
assembly = noAssembly(alignment.alignment, reference)
nextclade_denovo_json = OPTIONAL_FILE
}
per_read_stats = reads.map { meta, reads, index, stats ->
// the glob `"*read*stats.tsv.gz"` matches bamstats and fastcat per-read stats
[meta, file(stats.resolve("*read*stats.tsv.gz"))]
}
report = makeReport(
per_read_stats.map { meta, stats -> stats },
software_versions.collect(),
workflow_params,
variants.map{it[2]}.collect(),
coverage,
reference,
assembly,
nextclade_ref_json,
nextclade_denovo_json
)
emit:
per_read_stats
results = report.concat(
alignment.alignment.map{it[2]}.collect(),
alignment.alignment.map{it[3]}.collect(),
variants.map{it[2]}.collect(),
variants.map{it[3]}.collect(),
consensus.map{it[2]}.collect(),
coverage,
assembly.map{it[2]}.collect(),
software_versions,
workflow_params
)
// TODO: use something more useful as telemetry
telemetry = workflow_params
}
// entrypoint workflow
WorkflowMain.initialise(workflow, params, log)
workflow {
Pinguscript.ping_start(nextflow, workflow, params)
// warn the user if overriding the basecall models found in the inputs
if (params.override_basecaller_cfg) {
log.warn \
"Overriding basecall model with '${params.override_basecaller_cfg}'."
}
if (params.reference == null){
params.remove('reference')
params._reference = projectDir.resolve("./data/references/MT903344.1.fasta").toString()
params._genbank = projectDir.resolve("./data/references/MT903344.1.gb").toString()
} else {
path = projectDir.resolve("./data/references/"+params.reference+".fasta").toString()
params._reference = file(path, type: "file", checkIfExists:true).toString()
path_genbank = projectDir.resolve("./data/references/"+params.reference+".gb").toString()
params._genbank = file(path_genbank, type: "file", checkIfExists:true).toString()
params.remove('reference')
}
Map ingress_args = [
"sample": params.sample,
"sample_sheet": null,
"stats": true,
"per_read_stats": true,
"allow_multiple_basecall_models": false,
]
if (params.fastq) {
samples = fastq_ingress(ingress_args + [
"input":params.fastq,
])
| map { meta, reads, stats -> [meta, reads, OPTIONAL_FILE, stats] }
} else {
samples = xam_ingress(ingress_args + [
"input":params.bam,
"keep_unaligned":true,
])
}
// make sure we only got one sample
samples
| toList
| subscribe {
if (it.size() > 1) {
error "Found more than one barcode / sample; this is currently not supported."
}
}
pipeline(samples, params._reference, params._genbank)
Channel.empty()
| mix(
pipeline.out.per_read_stats
| map { meta, stats -> [stats, "${meta.alias}.per-read-stats.tsv.gz"] },
pipeline.out.results
| map { it -> [it, null] },
)
| output
}
workflow.onComplete {
Pinguscript.ping_complete(nextflow, workflow, params)
}
workflow.onError {
Pinguscript.ping_error(nextflow, workflow, params)
}