-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.nf
executable file
·491 lines (411 loc) · 14.3 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/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'
OPTIONAL_FILE = file("$projectDir/data/OPTIONAL_FILE")
process alignReads {
label "wfflu"
cpus 2
input:
tuple val(meta), path("reads.fastq.gz")
path "reference.fasta"
output:
tuple val(meta), path("align.bam"), path("align.bam.bai"), emit: alignments
tuple path("align.bamstats"), path("align.bam.summary"), emit: bamstats
shell:
"""
mini_align -i reads.fastq.gz -r reference.fasta -p align_tmp -t 2 -m
# keep only mapped reads
samtools view --write-index -F 4 align_tmp.bam -o align.bam##idx##align.bam.bai
# get stats from bam
stats_from_bam -o align.bamstats -s align.bam.summary -t 2 align.bam
"""
}
process coverageCalc {
label "wfflu"
cpus 1
input:
tuple val(meta), path("align.bam"), path("align.bam.bai")
output:
tuple val(meta), path("depth.txt")
"""
samtools depth -aa align.bam -Q 20 -q 1 > depth.txt
"""
}
process downSample {
label 'wfflu'
cpus 1
input:
tuple val(meta), path("align.bam"), path("align.bam.bai")
path "reference.fasta"
output:
tuple val(meta), path("all_merged.sorted.bam"), path("all_merged.sorted.bam.bai"), emit: alignments
"""
# get region info from fasta
samtools faidx reference.fasta
cut -f1-2 reference.fasta.fai > regions.txt
# for every region we're going to downsample separatley
while read -r region length;
do
# get upper and lower bounds of reference span
upper=`echo \$((\${length}+(\${length}*10/100)))`
lower=`echo \$((\${length}-(\${length}*10/100)))`
# filter reads in region and covering region
samtools view -bh align.bam -e "length(seq)>\${lower} && length(seq)<\${upper}" \${region} > \${region}.bam;
# ignore regions with no reads
count=`samtools view -c \${region}.bam`
if [ "\${count}" -eq "0" ];
then
echo "no reads in \${region} so continuing"
cp \${region}.bam \${region}_all.bam
continue;
fi
lines=( ${params.downsample} / 2 )
# get header
samtools view -H \${region}.bam > \${region}_all.sam
samtools view -F16 \${region}.bam | shuf -n \${lines} >> \${region}_all.sam
samtools view -f16 \${region}.bam | shuf -n \${lines} >> \${region}_all.sam
samtools view -bh \${region}_all.sam > \${region}_all.bam
done < regions.txt
samtools merge all_merged.bam *_all.bam
samtools sort all_merged.bam > all_merged.sorted.bam
samtools index all_merged.sorted.bam
echo "done"
"""
}
process medakaVariants {
label "medaka"
cpus 1
input:
tuple val(meta), path("downsample.bam"), path("downsample.bam.bai")
path("reference.fasta")
output:
tuple val(meta), path("variants.annotated.filtered.vcf")
script:
// 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`)
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."
}
"""
medaka consensus downsample.bam consensus.hdf --model "${basecall_model}:consensus"
medaka variant --gvcf reference.fasta consensus.hdf variants.vcf --verbose
medaka tools annotate --debug --pad 25 variants.vcf reference.fasta downsample.bam variants.annotated.vcf
bcftools filter -e "ALT='.'" variants.annotated.vcf | bcftools filter -o variants.annotated.filtered.vcf -O v -e "INFO/DP<${params.min_coverage}" -
"""
}
process makeConsensus {
label "wfflu"
cpus 1
input:
tuple val(meta), path("variants.annotated.filtered.vcf"), path("depth.txt")
path "reference.fasta"
output:
tuple val(meta), path("draft.consensus.fasta")
"""
awk '{if (\$3<${params.min_coverage}) print \$1"\t"\$2+1}' depth.txt > mask.regions
bgzip variants.annotated.filtered.vcf
tabix variants.annotated.filtered.vcf.gz
bcftools consensus --mask mask.regions --mark-del '-' --mark-ins lc --fasta-ref reference.fasta -o draft.consensus.fasta variants.annotated.filtered.vcf.gz
"""
}
process typeFlu {
label "wfflutyping"
cpus 1
input:
tuple val(meta), path("consensus.fasta")
path("blast_db")
output:
tuple val(meta), path("insaflu.typing.txt"), emit: typing
path "abricate.version", emit: version
"""
abricate --version | sed 's/ /,/' > abricate.version
abricate --datadir blast_db --db insaflu -minid 70 -mincov 60 --quiet consensus.fasta 1> insaflu.typing.txt
"""
}
process processType {
label "wfflu"
cpus 1
input:
tuple val(meta), path("insaflu.typing.txt")
output:
tuple val(meta), path("processed_type.json")
script:
"""
workflow-glue process_abricate --typing insaflu.typing.txt --output processed_type.json
"""
}
process prepNextclade {
label "wfflu"
cpus 1
input:
tuple val(meta), path("typing.json"), path("consensus.fasta")
path "nextclade_data"
output:
path "datasets", optional: true
script:
String alias = meta.alias
"""
mkdir datasets
workflow-glue nextclade_helper \
--typing typing.json \
--nextclade_datasets "nextclade_data" \
--consensus "consensus.fasta" \
--sample_alias "${alias}"
if [ -z "\$(ls -A datasets)" ]; then
rm -r datasets
fi
"""
}
process nextclade {
label "nextclade"
cpus 1
input:
tuple val(dataset), path(files)
output:
tuple val(dataset), path("${dataset}/${dataset}.json"), optional: true
script:
"""
mkdir ${dataset}
cat *.fasta > ${dataset}/${dataset}.consensus.fasta
if [[ "${dataset}" != "flu_h1n1pdm_na" ]]; then
nextclade dataset get --name \"${dataset}\" --output-dir "nextclade_datasets/${dataset}"
else
nextclade dataset get --name \"${dataset}\" --output-dir "nextclade_datasets/${dataset}" --reference MW626056
fi
nextclade run --input-dataset nextclade_datasets/${dataset} --output-all=${dataset}/ ${dataset}/${dataset}.consensus.fasta
mv ${dataset}/nextclade.json ${dataset}/${dataset}.json
"""
}
process getVersions {
label "wfflu"
cpus 1
output:
path "versions.txt"
script:
"""
python -c "import pysam; print(f'pysam,{pysam.__version__}')" >> versions.txt
fastcat --version | sed 's/^/fastcat,/' >> versions.txt
bcftools --version | head -1 | sed 's/ /,/' >> versions.txt
samtools --version | grep samtools | head -1 | sed 's/ /,/' >> versions.txt
"""
}
process getParams {
label "wfflu"
cpus 1
output:
path "params.json"
script:
def paramsJSON = new JsonBuilder(params).toPrettyString()
"""
# Output nextflow params object to JSON
echo '$paramsJSON' > params.json
"""
}
process collectFilesInDir {
label "wfflu"
cpus 1
input: tuple val(meta), path("staging_dir/*"), val(dirname)
output: tuple val(meta), path(dirname)
script:
"""
mv staging_dir $dirname
"""
}
process makeReport {
label "wf_common"
cpus 1
input:
val metadata
path stats, stageAs: "stats_*"
path "data/*"
path "nextclade/*"
path nextclade_datasets
path "versions/*"
path "params.json"
output:
tuple path("wf-flu-*.html"), path("wf-flu-results.csv")
script:
report_name = "wf-flu-report.html"
def metadata = new JsonBuilder(metadata).toPrettyString()
"""
echo '${metadata}' > metadata.json
workflow-glue report "${report_name}"\
--data data \
--stats ${stats} \
--versions versions \
--params params.json \
--nextclade_files nextclade/* \
--nextclade_datasets "${nextclade_datasets}" \
--metadata metadata.json \
--workflow_version ${workflow.manifest.version}
"""
}
// 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.
// The process takes a tuple containing the filename and the name of a sub-directory to
// put the file into. If the latter is `null`, puts it into the top-level directory.
process output {
// publish inputs to output directory
label "wfflu"
cpus 1
publishDir (
params.out_dir,
mode: "copy",
saveAs: { dirname ? "$dirname/$fname" : fname }
)
input:
tuple path(fname), val(dirname)
output:
path fname
"""
"""
}
// workflow module
workflow pipeline {
take:
samples
reference
blastdb
nextclade_data
main:
samples.multiMap{ meta, path, stats ->
meta: meta
stats: stats
}.set { for_report }
stats = for_report.stats.collect()
alignment = alignReads(samples.map{ meta, reads, stats -> [ meta,reads ] }, reference)
coverage = coverageCalc(alignment.alignments)
// do crude downsampling
if (params.rbk){
println("RBK data - NOT Downsampling!!!")
downsample = alignment
} else if (params.downsample != null){
println("Downsampling!!!")
downsample = downSample(alignment.alignments, reference)
} else {
println("NOT Downsampling!!!")
downsample = alignment
}
variants = medakaVariants(downsample.alignments, reference)
for_draft = variants.join(coverage)
draft = makeConsensus(for_draft, reference)
flu_type = typeFlu(draft, blastdb)
processed_type = processType(flu_type.typing)
nextclade_prep = prepNextclade(
processed_type.join(draft, remainder: true),
nextclade_data
)
nextclade_datasets = nextclade_prep
| map { file(it.resolve("**"), type: "file") }
| flatten
| map { [it.parent.name, it] }
| groupTuple
nextclade_result = nextclade(nextclade_datasets)
software_versions = getVersions()
software_versions = software_versions.mix(flu_type.version.first()) | collectFile()
workflow_params = getParams()
// output_alignments = alignment.alignments.map{ it -> return tuple(it[1], it[2]) }
// get all the per sample results together
ch_per_sample_results = samples
| join(coverage)
| join(flu_type.typing)
| join(processed_type)
// collect results into a directory for the sample directory to avoid collisions
ch_results_for_report = ch_per_sample_results
| map {
meta = it[0]
rest = it[1..-1]
[meta, rest, meta.alias]
}
| collectFilesInDir
| map { meta, dirname -> dirname }
report = makeReport(
for_report.meta.collect(),
stats,
ch_results_for_report | collect,
nextclade_result.map{it -> it[1]}.ifEmpty(OPTIONAL_FILE).collect(),
nextclade_data,
software_versions.collect(),
workflow_params
)
// create channel with files to publish; the channel will have the shape `[file,
// name of sub-dir to be published in]`.
ch_to_publish = Channel.empty()
| mix(
software_versions | map { [it, null] },
workflow_params | map { [it, null] },
report | map { [it[0] , null] },
report | map { [it[1], null] },
alignment.alignments
| map { meta, bam, bai -> [bam, "$meta.alias/alignments"] },
variants
| map { meta, vcf -> [vcf, "$meta.alias/variants"]},
coverage
| map { meta, depth -> [depth, "$meta.alias/coverage"]},
draft
| map { meta, fa -> [fa, "$meta.alias/consensus"]},
flu_type.typing
| map { meta, json -> [json, "$meta.alias/typing"]}
)
emit:
results = ch_to_publish
}
// 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}'."
}
samples = fastq_ingress([
"input": params.fastq,
"stats": true,
"sample_sheet": params.sample_sheet,
"allow_multiple_basecall_models": false,
])
//get reference
if (params.reference == null){
params.remove('reference')
params._reference = projectDir.resolve("./data/primer_schemes/V1/consensus_irma.fasta").toString()
} else {
params._reference = file(params.reference, type: "file", checkIfExists:true).toString()
params.remove('reference')
}
//get db
if (params.blastdb == null){
params.remove('blastdb')
params._blastdb = projectDir.resolve("./data/primer_schemes/V1/blastdb").toString()
} else {
params._blastdb = file(params.blastdb, type: "dir", checkIfExists:true).toString()
params.remove('blastdb')
}
nextclade_data = projectDir.resolve("./data/nextclade.csv").toString()
pipeline(samples, params._reference, params._blastdb, nextclade_data)
pipeline.out.results
| toList
| flatMap
| output
}
workflow.onComplete {
Pinguscript.ping_complete(nextflow, workflow, params)
}
workflow.onError {
Pinguscript.ping_error(nextflow, workflow, params)
}