-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16SProcessing.nf
440 lines (347 loc) · 11.2 KB
/
16SProcessing.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
#!/usr/bin/env nextflow
nextflow.enable.dsl=2
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
BORG/vsearch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Github : https://github.com/bio-ontology-research-group/16SProcessing
----------------------------------------------------------------------------------------
*/
params.in_dir = './' // Default input directory
params.out_dir = 'results' // Default output directory
params.skip_cutadapt = false
params.fwd_primer = 'CCTACGGGNGGCWGCAG' // Default forward primer sequence
params.rev_primer = 'GGACTACNVGGGTWTCTAAT' // Default reverse primer sequence
params.db_path = "/data/gold.fasta"
params.rdp_path = "/data/rdp_16s_v18.fa"
params.python_paths = "/data/"
params.single_end = false // false indicates paired-end by default, set to true for single-end
/*
================================================================================
Preprocessing and QC
================================================================================
*/
process CutAdapt {
label 'preprocess'
tag "${pair_id}"
publishDir "${params.out_dir}_results/trimmed_reads", mode: 'copy'
input:
tuple val(pair_id), path(reads)
output:
tuple val(pair_id), path("*_trimmed.fastq.gz"), emit: trimmed_reads
script:
if (params.single_end) {
def read1 = reads
"""
cutadapt -g ${params.fwd_primer} -o ${pair_id}_trimmed.fastq.gz ${read1}
"""
} else {
def (read1, read2) = reads
"""
cutadapt -g ${params.fwd_primer} -G ${params.rev_primer} \
-o ${pair_id}_L001_R1_001_trimmed.fastq.gz \
-p ${pair_id}_L001_R2_001_trimmed.fastq.gz \
${read1} ${read2}
"""
}
}
process FastQC {
label 'preprocess'
tag "${pair_id}"
publishDir "${params.out_dir}_results/fastQC", mode: 'copy'
input:
tuple val(pair_id), path(read_files)
output:
path "*.html", emit: fastqc_html
path "*.zip", emit: fastqc_zip
script:
if (params.single_end) {
def read1 = read_files
"""
fastqc ${read1}
"""
} else {
def (read1, read2) = read_files
"""
fastqc ${read1} ${read2}
"""
}
}
/*
================================================================================
Merging
================================================================================
*/
process VsearchMerge {
label 'vsearch'
tag "${pair_id}"
publishDir "${params.out_dir}_results/intermediate_files", mode: 'copy'
input:
tuple val(pair_id), path(trimmed_reads)
output:
tuple val(pair_id), path("*.merged.fastq"), emit: merged_fastq
path "vsearch_merge_log_${pair_id}.txt", emit: merge_log // Added line to emit the log file
script:
def (read1, read2) = trimmed_reads
"""
vsearch --fastq_mergepairs ${read1} --reverse ${read2} --fastqout ${pair_id}.merged.fastq &> vsearch_merge_log_${pair_id}.txt
"""
}
process VsearchStats {
label 'vsearch'
tag "${pair_id}"
publishDir "${params.out_dir}_results/intermediate_files", mode: 'copy'
input:
tuple val(pair_id), path(merged_fastq)
output:
path "${pair_id}.stats"
script:
"""
vsearch --fastq_eestats ${merged_fastq} --output ${pair_id}.stats
"""
}
process VsearchFilter {
label 'vsearch'
tag "${pair_id}"
publishDir "${params.out_dir}_results/intermediate_files", mode: 'copy'
input:
tuple val (pair_id), path(merged_fastq)
output:
tuple val(pair_id), path("*.filtered.fasta"), emit: filtered_fasta
script:
"""
vsearch --fastq_filter ${merged_fastq} --fastq_maxee 1.0 --fastq_minlen 10 --fastq_maxlen 500 --fastq_maxns 0 --fastaout ${pair_id}.filtered.fasta --fasta_width 0
"""
}
process VsearchDereplicate {
label 'vsearch'
tag "${pair_id}"
publishDir "${params.out_dir}_results/intermediate_files", mode: 'copy'
input:
tuple val(pair_id), path(filtered_fasta)
output:
path "${pair_id}.derep.fasta"
script:
"""
vsearch --derep_fulllength ${filtered_fasta} --strand plus --output ${pair_id}.derep.fasta --sizeout --relabel ${pair_id}. --fasta_width 0
"""
}
process MergeAll{
publishDir "${params.out_dir}_results/merged_intermediates", mode: 'copy'
input:
path derep_fasta_files
output:
path "all.fasta"
script:
"""
cat ${derep_fasta_files.join(' ')} > all.fasta
"""
}
process VsearchDerepAll{
label 'vsearch'
publishDir "${params.out_dir}_results/merged_intermediates", mode: 'copy'
input:
path all_merged_fasta
output:
path "derep.fasta"
script:
"""
vsearch --derep_fulllength ${all_merged_fasta} --sizein --sizeout --fasta_width 0 --uc all.derep.uc --output derep.fasta
"""
}
/*
================================================================================
Clustering
================================================================================
*/
process VsearchCluster{
label 'vsearch'
publishDir "${params.out_dir}_results/merged_intermediates", mode: 'copy'
input:
path all_derep_fasta
output:
path "centroids.fasta"
script:
"""
vsearch --cluster_size derep.fasta --id 0.98 --strand plus --sizein --sizeout --fasta_width 0 --centroids centroids.fasta
"""
}
process SwarmCluster{
label 'swarm'
publishDir "${params.out_dir}_results/merged_intermediates", mode: 'copy'
input:
path all_derep_fasta
path centroids
output:
path "centroids.fasta"
script:
"""
swarm ${all_derep_fasta} --differences 1 --fastidious --seeds ${centroids} --usearch-abundance --output /dev/null
"""
}
process SingletonRemoval{
label 'vsearch'
publishDir "${params.out_dir}_results/merged_intermediates", mode: 'copy'
input:
path swarm_centroids
output:
path "sorted.fasta"
script:
"""
vsearch --sortbysize ${swarm_centroids} --sizein --sizeout --fasta_width 0 --minsize 2 --output sorted.fasta
"""
}
process DenovoChimeraRemoval{
label 'vsearch'
publishDir "${params.out_dir}_results/merged_intermediates", mode: 'copy'
input:
path nonsingleton_clusters
output:
path "denovo.nonchimeras.fasta"
script:
"""
vsearch --uchime_denovo ${nonsingleton_clusters} --sizein --sizeout --fasta_width 0 --qmask none --nonchimeras denovo.nonchimeras.fasta
"""
}
process ReferenceChimeraRemoval{
label 'vsearch'
publishDir "${params.out_dir}_results/merged_intermediates", mode: 'copy'
input:
path denovo_nonchimeras
output:
path "nonchimeras.fasta"
script:
"""
vsearch --uchime_ref ${denovo_nonchimeras} --db ${params.db_path} --sizein --sizeout --fasta_width 0 --qmask none --dbmask none --nonchimeras nonchimeras.fasta
"""
}
/*
================================================================================
OTUs
================================================================================
*/
process RelabelOTUs{
label 'vsearch'
publishDir "${params.out_dir}_results/OTU_tables", mode: 'copy'
input:
path ref_nonchimeras
output:
path "otus.fasta"
script:
"""
vsearch --fastx_filter ${ref_nonchimeras} --sizein --sizeout --fasta_width 0 --relabel OTU_ --fastaout otus.fasta
"""
}
process MapOTUs{
label 'vsearch'
publishDir "${params.out_dir}_results/OTU_tables", mode: 'copy'
input:
path all_merged_fasta
path relabeled_otus
output:
path "otutab.txt"
script:
"""
vsearch --usearch_global ${all_merged_fasta} --db ${relabeled_otus} --id 0.98 \
--strand plus --sizein --sizeout --fasta_width 0 --qmask none --dbmask none \
--otutabout otutab.txt
"""
}
process ClassifyOTUs{
label 'vsearch'
publishDir "${params.out_dir}_results/OTU_tables", mode: 'copy'
input:
path relabeled_otus
output:
path "reads.sintax"
script:
"""
vsearch -sintax ${relabeled_otus} -db ${params.rdp_path} -tabbedout reads.sintax \
-strand both --sintax_cutoff 0.8
"""
}
/*
================================================================================
OTU Table Modification
================================================================================
*/
process RelativeAbundance{
label 'python'
publishDir "${params.out_dir}_results/OTU_tables", mode: 'copy'
input:
path otu_table
output:
path "otutab_relative.txt"
script:
"""
python3 ${params.python_paths}count_relative.py $otu_table > otutab_relative.txt
"""
}
process MapOTUNames{
label 'python'
publishDir "${params.out_dir}_results/OTU_tables", mode: 'copy'
input:
path relative_abund
path sintax_classification
output:
path "otutab_relative_withtaxa.txt"
script:
"""
python3 ${params.python_paths}otu_mapping.py $relative_abund $sintax_classification
"""
}
process MergeTaxa{
label 'python'
publishDir "${params.out_dir}_results/OTU_tables", mode: 'copy'
input:
path mapped_relative_otus
output:
path "otutab_relative_withtaxa_merged.tsv"
script:
"""
python3 ${params.python_paths}merge_abundance.py $mapped_relative_otus
"""
}
workflow {
read_files = params.single_end ?
Channel.fromPath("${params.in_dir}/*.fastq.gz")
.map { file -> [file.baseName, file] } :
Channel.fromFilePairs("${params.in_dir}/*_L001_R{1,2}_001.fastq.gz", size: 2)
// Preprocessing
if (!params.skip_cutadapt) {
CutAdapt(read_files)
FastQC(CutAdapt.out.trimmed_reads)
if (!params.single_end) {
vsearch_merge_out = VsearchMerge(CutAdapt.out.trimmed_reads)
} else {
vsearch_merge_out = CutAdapt.out.trimmed_reads
}
} else {
FastQC(read_files)
if (!params.single_end) {
vsearch_merge_out = VsearchMerge(read_files)
} else {
vsearch_merge_out = read_files
}
}
VsearchStats(vsearch_merge_out.merged_fastq)
vsearch_filter_out = VsearchFilter(vsearch_merge_out.merged_fastq)
derep_out = VsearchDereplicate(vsearch_filter_out)
derep_fasta_files = derep_out.collect()
all_merged_fasta = MergeAll(derep_fasta_files)
all_derep_fasta = VsearchDerepAll(all_merged_fasta)
//Cluster
centroids = VsearchCluster(all_derep_fasta)
swarm_centroids = SwarmCluster(all_derep_fasta, centroids)
nonsingleton_clusters = SingletonRemoval(swarm_centroids)
denovo_nonchimeras = DenovoChimeraRemoval(nonsingleton_clusters)
ref_nonchimeras = ReferenceChimeraRemoval(denovo_nonchimeras)
//OTUs
relabeled_otus = RelabelOTUs(ref_nonchimeras)
otu_table = MapOTUs(all_merged_fasta, relabeled_otus)
sintax_classification = ClassifyOTUs(relabeled_otus)
//OTU Table Modification
relative_abund = RelativeAbundance(otu_table)
mapped_relative_otus = MapOTUNames(relative_abund, sintax_classification)
merged_otus = MergeTaxa(mapped_relative_otus)
}