-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.nf
More file actions
392 lines (320 loc) · 9.08 KB
/
main.nf
File metadata and controls
392 lines (320 loc) · 9.08 KB
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
nextflow.enable.dsl=2
/*
* Required inputs
*/
if( !params.read1 || !params.read2 ) {
log.error """
Missing inputs.
Usage:
nextflow run main.nf -profile conda \\
--read1 <R1.fastq.gz> \\
--read2 <R2.fastq.gz> \\
--sample <sample_name> \\
--barcodes_fasta <barcodes_anchored.fasta> \\
--outdir results
"""
System.exit(1)
}
/*
* Single-sample input channel
*/
def r1 = file(params.read1)
def r2 = file(params.read2)
def barcodes_file = file(params.barcodes_fasta)
Channel
.of( tuple(params.sample, r1, r2) )
.set { ch_sample }
barcodes_ch = Channel.value(barcodes_file)
/*
* Processes
*/
process FASTP {
tag "${sample_id}"
publishDir "${params.outdir}/01_fastp/${sample_id}", mode: 'copy'
cpus params.cpus
input:
tuple val(sample_id), path(read1), path(read2)
output:
tuple val(sample_id), path("${sample_id}_1.fastp.fastq.gz"), path("${sample_id}_2.fastp.fastq.gz"), path("${sample_id}.fastp.html"), path("${sample_id}.fastp.json"), emit: reads
path("*.json"), emit: log
path("*.html")
script:
"""
fastp \
-i ${read1} -I ${read2} \
-o ${sample_id}_1.fastp.fastq.gz \
-O ${sample_id}_2.fastp.fastq.gz \
--detect_adapter_for_pe \
--cut_front --cut_tail \
--cut_window_size 4 \
--cut_mean_quality 20 \
--length_required 50 \
--n_base_limit 5 \
--thread ${task.cpus} \
--html ${sample_id}.fastp.html \
--json ${sample_id}.fastp.json
"""
}
process FLASH2_MERGE {
tag "${sample_id}"
publishDir "${params.outdir}/02_flash2/${sample_id}", mode: 'copy'
cpus params.cpus
input:
tuple val(sample_id), path(r1), path(r2), path(html), path(json)
output:
tuple val(sample_id), path("${sample_id}.extendedFrags.fastq.gz"), emit: reads
path "${sample_id}.flash.log", emit: log
script:
"""
flash2 \
-z \
-t ${task.cpus} \
-m ${params.flash_min_overlap} \
-M ${params.flash_max_overlap} \
-x ${params.flash_mismatch} \
-p ${params.flash_phred} \
-o ${sample_id} \
${r1} ${r2} > ${sample_id}.flash.log 2>&1
"""
}
process ORIENT_READS {
tag "${sample_id}"
publishDir "${params.outdir}/03_orient/${sample_id}", mode: 'copy'
cpus params.cpus
input:
tuple val(sample_id), path(merged)
path barcodes
output:
tuple val(sample_id), path("${sample_id}.merged.oriented.fastq.gz"), path("${sample_id}.orient.log"), emit: reads
path "${sample_id}.orient.log", emit: log
script:
"""
cutadapt \
-j ${task.cpus} \
--revcomp \
--no-trim \
-g file:${barcodes} \
-e ${params.cutadapt_e_orient} \
-a ${params.orient_3p_anchor} \
--discard-untrimmed \
-o ${sample_id}.merged.oriented.fastq.gz \
${merged} \
> ${sample_id}.orient.log
"""
}
process DEMUX {
tag "${sample_id}"
publishDir "${params.outdir}/04_demux/${sample_id}", mode: 'copy'
cpus params.cpus
input:
tuple val(sample_id), path(oriented), path(orient_log)
path barcodes
output:
tuple val(sample_id), path("demux/*.fastq.gz"), path("${sample_id}_demux.cutadapt.log"), emit: reads
path "${sample_id}_demux.cutadapt.log", emit: log
script:
"""
mkdir -p demux
cutadapt \
-j ${task.cpus} \
--overlap 9 \
-e ${params.cutadapt_e_demux} \
-g file:${barcodes} \
-o demux/{name}.fastq.gz \
--untrimmed-output demux/unknown.fastq.gz \
${oriented} \
> ${sample_id}_demux.cutadapt.log
"""
}
process EXTRACT_LIBRARY {
tag "${sample_id}:${bin_fastq.simpleName}"
publishDir "${params.outdir}/05_extract/${sample_id}", mode: 'copy'
cpus params.cpus
input:
tuple val(sample_id), path(bin_fastq)
output:
tuple val(sample_id), path("${bin_fastq.simpleName}.library.fastq.gz"), path("${bin_fastq.simpleName}.extract.log"), emit: reads
path "*.extract.log", emit: log
path "${bin_fastq.simpleName}.untrimmed.fastq.gz", optional: true
path "${bin_fastq.simpleName}.toolong.fastq.gz", optional: true
path "${bin_fastq.simpleName}.tooshort.fastq.gz", optional: true
script:
"""
cutadapt \
-j ${task.cpus} \
--no-indels \
-e ${params.cutadapt_e} \
--overlap ${params.extract_overlap} \
-g "${params.left_flank}...${params.right_flank}" \
--untrimmed-output ${bin_fastq.simpleName}.untrimmed.fastq.gz \
-m ${params.lib_min_len} -M ${params.lib_max_len} \
--too-long-output ${bin_fastq.simpleName}.toolong.fastq.gz \
--too-short-output ${bin_fastq.simpleName}.tooshort.fastq.gz \
-o ${bin_fastq.simpleName}.library.fastq.gz \
${bin_fastq} \
> ${bin_fastq.simpleName}.extract.log
"""
}
process COUNT_LIBRARY {
tag "${sample_id}:bin${bin_num}"
publishDir "${params.outdir}/06_counts/${sample_id}", mode: 'copy'
cpus 1
input:
tuple val(sample_id), val(bin_num), path(lib_fastq)
output:
tuple val(sample_id), path("${lib_fastq.simpleName}.counts.tsv")
script:
"""
python - << 'PY'
import gzip
from collections import Counter
lib_path = "${lib_fastq}"
out_path = "${lib_fastq.simpleName}.counts.tsv"
bin_num = "${bin_num}"
counts = Counter()
with gzip.open(lib_path, "rt") as fh:
for i, line in enumerate(fh):
if i % 4 == 1:
seq = line.strip()
if seq:
counts[seq] += 1
with open(out_path, "w") as out:
out.write("bin\\tsequence\\tcount\\n")
for seq, c in sorted(counts.items(), key=lambda x: (-x[1], x[0])):
out.write(f"{bin_num}\\t{seq}\\t{c}\\n")
PY
"""
}
process COUNT_WHOLE_READS {
tag "${sample_id}:bin${bin_num}"
publishDir "${params.outdir}/06_whole_counts/${sample_id}", mode: 'copy'
cpus 1
input:
tuple val(sample_id), val(bin_num), path(fastq)
output:
tuple val(sample_id), path("${fastq.simpleName}.counts.tsv")
script:
"""
python - << 'PY'
import gzip
from collections import Counter
fastq_path = "${fastq}"
out_path = "${fastq.simpleName}.counts.tsv"
bin_num = "${bin_num}"
counts = Counter()
with gzip.open(fastq_path, "rt") as fh:
for i, line in enumerate(fh):
if i % 4 == 1:
seq = line.strip()
if seq:
counts[seq] += 1
with open(out_path, "w") as out:
out.write("bin\\tsequence\\tcount\\n")
for seq, c in sorted(counts.items(), key=lambda x: (-x[1], x[0])):
out.write(f"{bin_num}\\t{seq}\\t{c}\\n")
PY
"""
}
process MERGE_COUNTS {
tag "${sample_id}"
publishDir "${params.outdir}/07_merged_counts/${sample_id}", mode: 'copy'
cpus 1
input:
tuple val(sample_id), path(count_files)
output:
path("all_bins.counts.tsv")
script:
def files_str = count_files.join(' ')
"""
set -euo pipefail
# Grab header from first file
first_file=\$(echo "${files_str}" | awk '{print \$1}')
head -n 1 \$first_file > all_bins.counts.tsv
# Loop and skip header
for f in ${files_str}; do
tail -n +2 "\$f" >> all_bins.counts.tsv
done
"""
}
process MERGE_WHOLE_COUNTS {
tag "${sample_id}"
publishDir "${params.outdir}/07_merged_whole_counts/${sample_id}", mode: 'copy'
cpus 1
input:
tuple val(sample_id), path(count_files)
output:
path("all_bins.counts.tsv")
script:
def files_str = count_files.join(' ')
"""
set -euo pipefail
# Grab header from first file
first_file=\$(echo "${files_str}" | awk '{print \$1}')
head -n 1 \$first_file > all_bins.counts.tsv
# Loop and skip header
for f in ${files_str}; do
tail -n +2 "\$f" >> all_bins.counts.tsv
done
"""
}
process MULTIQC {
publishDir "${params.outdir}/00_reports", mode: 'copy'
input:
path multiqc_files
output:
path "multiqc_report.html"
script:
"""
multiqc .
"""
}
/*
* Workflow wiring
*/
workflow {
ch_fastp = FASTP(ch_sample)
ch_merged = FLASH2_MERGE(ch_fastp.reads)
ch_oriented = ORIENT_READS(ch_merged.reads, barcodes_ch)
ch_demux = DEMUX(ch_oriented.reads, barcodes_ch)
// Flatten demux bins, skip unknown
ch_bins = ch_demux.reads.flatMap { sample_id, files, demux_log ->
files
.findAll { it.name != 'unknown.fastq.gz' }
.collect { f -> tuple(sample_id, f) }
}
// Count whole (demuxed) reads per bin
if (params.count_whole_reads) {
ch_bins_numbered = ch_bins.map { sample_id, bin_fastq ->
def m = (bin_fastq.simpleName =~ /(\d+)/)
def bin = m.find() ? m.group(1) : 'NA'
tuple(sample_id, bin, bin_fastq)
}
COUNT_WHOLE_READS(ch_bins_numbered)
.groupTuple()
.map { sample_id, files -> tuple(sample_id, files) }
| MERGE_WHOLE_COUNTS
}
// Gather base logs
ch_reports = Channel.empty()
.mix(ch_fastp.log)
.mix(ch_merged.log)
.mix(ch_oriented.log)
.mix(ch_demux.log)
// Extract library region and count per bin
if (params.count_library) {
ch_lib = EXTRACT_LIBRARY(ch_bins)
ch_lib.reads
.map { sample_id, lib_fastq, extract_log ->
def m = (lib_fastq.simpleName =~ /(\d+)/)
def bin = m.find() ? m.group(1) : 'NA'
tuple(sample_id, bin, lib_fastq)
}
| COUNT_LIBRARY
| groupTuple
| map { sample_id, files -> tuple(sample_id, files) }
| MERGE_COUNTS
ch_reports = ch_reports.mix(ch_lib.log)
}
// Generate Report
MULTIQC(ch_reports.collect())
}