forked from thanhleviet/nf-ont-taxamap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nf
480 lines (371 loc) · 11.1 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
#!/usr/bin/env nextflow
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.attribute.BasicFileAttributes
include { paramsHelp } from 'plugin/nf-validation'
include { paramsSummaryLog } from 'plugin/nf-validation'
include { scanFolder; getMostRecentXlsxFile } from './lib/utils.nf'
log.info paramsSummaryLog(workflow)
if (params.help) {
log.info paramsHelp("nextflow run thanhleviet/nf-ont-metamap --input /path/to/a/folder/of/barcodes")
exit 0
}
process CONCATENATE {
tag {sample_id}
conda projectDir + '/env/conda-env.yml'
cpus 4
input:
tuple val(sample_id), path(barcode_path)
output:
tuple val(sample_id), path("${sample_id}.fastq.gz")
script:
"""
fastcat ${barcode_path} | pigz -p ${task.cpus} - > ${sample_id}.fastq.gz
"""
stub:
"""
fastcat --help
touch ${sample_id}.fastq.gz
"""
}
process DEHUMAN {
tag {sample_id}
conda projectDir + '/env/conda-env.yml'
cpus 32
memory '64.GB'
input:
tuple val(sample_id), path(reads)
output:
tuple val(sample_id), path("${sample_id}.non_host.fastq.gz")
script:
"""
minimap2 -ax map-ont -t $task.cpus $params.human_ref $reads | samtools fastq -f 4 | pigz - > ${sample_id}.non_host.fastq.gz
"""
stub:
"""
minimap2 --version
touch ${sample_id}.non_host.fastq.gz
"""
}
process FILTER_BY_LENGTH {
tag {sample_id}
conda projectDir + '/env/conda-env.yml'
cpus 4
input:
tuple val(sample_id), path(reads)
output:
tuple val(sample_id), path("${sample_id}.filtered.${params.min_read_length}.fastq.gz"), emit: reads
tuple val(sample_id), path("*.json"), emit: stats
script:
"""
seqkit -j $task.cpus seq -m ${params.min_read_length} ${reads} | pigz - > ${sample_id}.filtered.${params.min_read_length}.fastq.gz
nanoq -s -vvv -j -r ${sample_id}.stats.pre_filtered.json -i ${reads}
nanoq -s -vvv -j -r ${sample_id}.stats.post_filtered.json -i ${sample_id}.filtered.${params.min_read_length}.fastq.gz
"""
stub:
"""
seqkit -h
touch ${sample_id}.filtered.${params.min_read_length}.fastq.gz
touch ${sample_id}.stats.pre_filtered.json
touch ${sample_id}.stats.post_filtered.json
"""
}
process FILTER_BY_TIME {
tag {sample_id}
conda projectDir + '/env/conda-env.yml'
cpus 2
input:
tuple val(sample_id), path(reads)
output:
tuple val(sample_id), path("${sample_id}.ontime.fastq.gz"), emit: reads
script:
"""
ontime -o ${sample_id}.ontime.fastq.gz --from ${params.ontime_from} --to ${params.ontime_to} ${reads}
"""
stub:
"""
ontime --help
touch ${sample_id}.ontime.fastq.gz
"""
}
process DEHUMAN2 {
tag {sample_id}
conda projectDir + '/env/conda-env.yml'
cpus 6
memory '12.GB'
input:
tuple val(sample_id), path(reads)
output:
tuple val(sample_id), path("${sample_id}.non_host.fastq.gz"), emit: fastq
path ("${sample_id}.flagstat"), emit: flagstat
script:
args = task.ext.args ?: ''
"""
minimap2 -ax map-ont $args -t $task.cpus $params.human_ref $reads | samtools view -Sb - > mapped.bam
samtools flagstat mapped.bam > ${sample_id}.flagstat
samtools fastq -f 4 mapped.bam | pigz - > ${sample_id}.non_host.fastq.gz
"""
stub:
"""
minimap2 --version
samtools --version
touch ${sample_id}.non_host.fastq.gz
touch ${sample_id}.flagstat
"""
}
process MINIMAP2_PAF {
tag {sample_id}
conda projectDir + '/env/conda-env.yml'
cpus 14
memory '64.GB'
input:
tuple val(sample_id), path(reads)
output:
tuple val(sample_id), path("${sample_id}.paf.gz")
script:
args = task.ext.args ?: ''
"""
minimap2 -cx map-ont $args -t $task.cpus $params.ref $reads --secondary=no | pigz - > ${sample_id}.paf.gz
"""
stub:
"""
minimap2 -h
touch ${sample_id}.paf.gz
"""
}
process AMR {
tag {sample_id}
conda projectDir + '/env/conda-env.yml'
cpus 32
memory '64.GB'
// maxForks 2
input:
tuple val(sample_id), path(reads)
output:
tuple val(sample_id), path("${sample_id}.*")
script:
"""
kma -i ${reads} -cge -ont -bcNano -md 1.0 -t ${task.cpus} -t_db ${params.amr_db} -o ${sample_id}
"""
stub:
"""
kma -v
touch ${sample_id}.{res,sam,map}
"""
}
process MINIMAP2_SAM {
tag {sample_id}
cpus 32
memory '64.GB'
// maxForks 2
input:
tuple val(sample_id), path(reads)
output:
tuple val(sample_id), path("${sample_id}.bam")
script:
"""
minimap2 -ax map-ont --split-prefix=tmp -N ${params.secondary_reads} -t $task.cpus $params.ref_sam $reads | samtools view -Sb - | samtools sort -@ $task.cpus -o ${sample_id}.bam -
"""
stub:
"""
minimap2 -v
touch ${sample_id}.bam
"""
}
process CLEAN_PAF {
tag {sample_id}
cpus 4
input:
tuple val(sample_id), path(paf)
output:
path("${sample_id}.cleaned.paf.gz")
shell:
'''
zcat !{sample_id}.paf.gz | awk 'OFS="," {print $1,$2,$3,$4,$6,$7,$8,$9,$10,$11,$12,$15,$17}' | sed 's/kraken:taxid|//g' | csvtk -H mutate2 -n cov -e '($4-$3)/$2*100' | csvtk -H mutate2 -n iden -e '$9/$10*100' | csvtk -H mutate2 -n score -e '2*(($14*$15)/($14+$15)*100)'| pigz - > !{sample_id}.cleaned.paf.gz
'''
stub:
"""
echo "Checked!"
touch ${sample_id}.cleaned.paf.gz
"""
}
process SAM2LCA {
tag {sample_id}
cpus 12
input:
tuple val(sample_id), path(bam)
output:
path("*.{csv,json,bam,report}")
script:
"""
samtools index ${bam}
sam2lca -d ${params.sam2lca_db} analyze -b -p ${task.cpus} ${bam}
"""
stub:
"""
samtools -v
"""
}
process PARSING_PAF {
publishDir "${params.outdir}/report", mode: "copy"
tag {sample_id}
conda projectDir + '/env/conda-report.yml'
cpus 4
input:
tuple val(sample_id), path(paf)
output:
path("*.csv")
script:
"""
paf2taxid.py -o ${sample_id} --score ${params.score} --min_read_length ${params.min_read_length} ${paf}
"""
stub:
"""
echo "Checked!"
touch ${sample_id}.csv
"""
}
process CREATE_EXCEL_REPORT {
publishDir "${params.outdir}", mode: "copy"
conda projectDir + '/env/conda-r.yml'
cpus 4
input:
path("*")
output:
path("*.xlsx")
script:
// Creates a new `java.util.Date` object and formats it as a string with the pattern `yyyy-MM-dd_HH`
_this_run = new java.util.Date().format('yyyy-MM-dd_HH')
// Sets the variable `run_name` to either the value of the `params.run_name` parameter,
// or to the formatted date string if `params.run_name` is null or undefined
run_name = workflow.runName
"""
Rscript --vanilla ${projectDir}/bin/create_excel_report.R \$PWD "report_${run_name}"
"""
stub:
"""
touch report.xlsx
"""
}
workflow wf_MINIMAP2_PAF {
take:
ch_reads
main:
MINIMAP2_PAF(ch_reads)
PARSING_PAF(MINIMAP2_PAF.out)
CLEAN_PAF(MINIMAP2_PAF.out)
emit:
// Maps the output of the process `PARSING_PAF.out` to a list of files that match the pattern `*_metaphlan_report.csv`
report = PARSING_PAF.out.map { it ->
def metaphlan_report = it.grep(~/.*_metaphlan_report\.csv$/)
if (metaphlan_report) {
metaphlan_report[0]
} else {
null
}
}
}
workflow wf_MINIMAP2_SAM {
take:
ch_reads
main:
MINIMAP2_SAM(ch_reads)
SAM2LCA(MINIMAP2_SAM.out)
}
// Scans the folder specified in the `params.input` parameter and retrieves the type and pattern of the files in the folder
def folderContents = scanFolder(params.input)
def input_type = folderContents[0][0]
def pattern = folderContents[0][1]
// Creates a channel `ch_input` from the files in the folder that match the pattern,
// and maps each file to a tuple containing its simple name and its full path
ch_input = Channel.fromPath("${params.input}/${pattern}", type: input_type)
.map {it -> tuple(it.simpleName, it)}
workflow {
ch_report = Channel.empty()
if (input_type == "dir") {
ch_reads = CONCATENATE(ch_input)
} else {
ch_reads = ch_input
}
if (!params.skip_filter_time) {
FILTER_BY_TIME(ch_reads)
ch_reads_filter_time = FILTER_BY_TIME.out.reads
} else {
ch_reads_filter_time = ch_reads
}
FILTER_BY_LENGTH(ch_reads_filter_time)
ch_report = ch_report.mix(FILTER_BY_LENGTH.out.stats.map {it -> it[1]}).ifEmpty([])
if (!params.skip_dehuman) {
DEHUMAN2(FILTER_BY_LENGTH.out.reads)
ch_dehuman_reads = DEHUMAN2.out.fastq
ch_report = ch_report.mix(DEHUMAN2.out.flagstat).ifEmpty([])
} else {
ch_dehuman_reads = FILTER_BY_LENGTH.out.reads
}
if (!params.skip_amr) {
AMR(ch_dehuman_reads)
// AMR.out.map{ it -> it[1].grep(~/.*\.res$/)}.view()
// Mixes the channel `ch_report` with the output of the process `AMR.out`
// and maps the output to a list of `.res` files
ch_report = ch_report.mix(
AMR.out.map {it ->
def resFiles = it[1].grep(~/.*\.res$/)
if (resFiles) {
resFiles[0]
} else {
null
}
}
).ifEmpty([])
}
if (!params.skip_paf) {
wf_MINIMAP2_PAF(ch_dehuman_reads)
}
// Mixes the channel `ch_report` with the output of the process `wf_MINIMAP2_PAF.out.report`
// and if the resulting channel is empty, sets it to an empty list
ch_report = ch_report.mix(wf_MINIMAP2_PAF.out.report).ifEmpty([])
// Not used for now
// if (!params.skip_sam) {
// wf_MINIMAP2_SAM(ch_dehuman_reads)
// }
// Collects all items from the channel `ch_report` and returns them as a list
ch_report = ch_report.collect()
CREATE_EXCEL_REPORT(ch_report)
}
workflow.onComplete {
report = getMostRecentXlsxFile(params.outdir.toString())
monitor_email = "thanh.le-viet@quadram.ac.uk"
if (params.email) {
email = params.email + "," + monitor_email
} else {
email = monitor_email
}
if (!params.no_run_stub) {
if (workflow.success) {
def msg = """\
Pipeline execution summary
---------------------------
Run Name : ${workflow.runName}
Completed at: ${workflow.complete}
Duration : ${workflow.duration}
Success : ${workflow.success}
exit status : ${workflow.exitStatus}
"""
.stripIndent()
sendMail {
to email
subject "BART pipeline completed!: " + workflow.runName
body msg
attach report
}
} else {
nextflow_log = "${workflow.launchDir}/.nextflow.log"
sendMail {
to monitor_email
subject "BART pipeline failed!: " + workflow.runName
body "Please check the log file for more details."
attach nextflow_log
}
}
}
}