-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.nf
More file actions
318 lines (311 loc) · 7.88 KB
/
modules.nf
File metadata and controls
318 lines (311 loc) · 7.88 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
// QC and trim reads
process FASTP {
publishDir "$params.outdir/fastp", mode: 'copy', pattern: '*html'
input:
tuple val(sample_id), path(reads)
output:
tuple val(sample_id), path('*fastq.gz'), emit: fastq
path '*fastp_report*', emit: report
script:
"""
fastp \
-i ${reads[0]} \
-I ${reads[1]} \
-o ${sample_id}_R1_trimmed.fastq.gz \
-O ${sample_id}_R2_trimmed.fastq.gz \
-h ${sample_id}_fastp_report.html \
-j ${sample_id}_fastp_report.json \
-w $task.cpus
"""
}
// Download and extract a pre-made Kraken DB from a URL
process KRAKENDB_DL_DB {
input:
val db_url
output:
path 'kraken_db'
script:
"""
wget $db_url
tar -xzvf *.tar.gz -C kraken_db
"""
}
// Download the NCBI taxonomy as a starting point for a custom Kraken DB
process KRAKENDB_DL_TAX {
output:
path 'kraken_db_tax'
script:
"""
kraken2-build --download-taxonomy --db kraken_db_tax
"""
}
// Download taxon-specific libraries as a starting point for a custom Kraken DB
process KRAKENDB_DL_LIB {
input:
val library
output:
path '*'
script:
"""
kraken2-build --download-library $library --db db_dir
mv db_dir/library/* .
rm -r db_dir
"""
}
// Combine separate dirs with libraries into one
process KRAKENDB_COMBINE_LIBS {
input:
path lib_dirs
output:
path 'lib_dir'
script:
"""
mkdir -p lib_dir
cp -rLv $lib_dirs lib_dir/
"""
}
// Add a set of custom genomes in a dir to a custom Kraken DB
process KRAKENDB_COMBINE_AND_ADD {
input:
path tax_dir
path lib_dir
path genomes_to_add_dir
output:
path "kraken_db_unbuilt"
script:
"""
mkdir -p kraken_db_unbuilt/library
cp -rLv ${tax_dir}/taxonomy kraken_db_unbuilt
cp -rLv ${lib_dir}/* kraken_db_unbuilt/library/
if [[ -d ${genomes_to_add_dir} ]]; then
for fasta in ${genomes_to_add_dir}/*fna; do
kraken2-build --add-to-library \$fasta --db kraken_db_unbuilt
done
fi
"""
}
// Build the final custom Kraken DB
process KRAKENDB_BUILD {
input:
path kraken_db_unbuilt
output:
path 'kraken_db'
script:
"""
mkdir kraken_db
cp -rLv $kraken_db_unbuilt/* kraken_db/
kraken2-build --build --db kraken_db --threads $task.cpus
"""
}
// Run Kraken to assign taxonomy to reads
process KRAKEN {
publishDir "$params.outdir/kraken", mode: 'copy', pattern: '*txt'
input:
tuple val(sample_id), path(reads)
path(db)
output:
tuple val(sample_id), path('*fastq'), emit: classified_fq
tuple val(sample_id), path('*report.txt'), emit: report
tuple val(sample_id), path('*main.txt'), emit: main
path '*report.txt', emit: report_path // For MultiQC, to avoid complications with tuple
script:
"""
kraken2 \
--db ${db} \
--report ${sample_id}_kraken-report.txt \
--output ${sample_id}_kraken-main.txt \
--classified-out ${sample_id}#.fastq \
--gzip-compressed \
--paired \
--threads $task.cpus \
${reads[0]} \
${reads[1]}
"""
}
// Build a Bracken DB
process BRACKENDB_BUILD {
input:
path kraken_db
val read_len
output:
path 'bracken_db'
script:
"""
cp -rLv $kraken_db bracken_db
bracken-build -d bracken_db -l $read_len -t $task.cpus
"""
}
// Run Bracken
process BRACKEN {
publishDir "$params.outdir/bracken", mode: 'copy', pattern: '*txt'
input:
tuple val(sample_id), path(kraken_report)
path bracken_db
val tax_level
val min_reads
val read_len
output:
path '*bracken*txt'
script:
"""
bracken \
-i ${kraken_report} \
-d ${bracken_db} \
-o ${sample_id}_bracken-out.txt \
-w ${sample_id}_bracken-report.txt \
-r ${read_len} \
-l ${tax_level} \
-t ${min_reads}
"""
}
// Assemble reads
process ASSEMBLY {
input:
tuple val(sample_id), path(reads)
output:
path "${sample_id}_scaffolds.fasta", emit: fasta
path "${sample_id}_contigs.fasta"
path "${sample_id}_spades.log"
script:
def memory_gb = MemoryUnit.of("${task.memory}").toUnit('GB')
"""
spades.py \
-1 ${reads[0]} \
-2 ${reads[1]} \
-o outdir \
--only-assembler \
--meta \
--threads $task.cpus \
--memory $memory_gb
mv outdir/contigs.fasta ${sample_id}_contigs.fasta
mv outdir/scaffolds.fasta ${sample_id}_scaffolds.fasta
mv outdir/spades.log ${sample_id}_spades.log
"""
}
process MULTIQC {
publishDir "$params.outdir/multiqc", mode: 'copy'
input:
path multiqc_input
output:
path 'multiqc_report.html'
script:
"""
multiqc .
"""
}
process HOST_INDEX {
publishDir "${params.outdir}/hostindex", mode: "copy"
input:
path host_fasta
output:
path 'host_index_dir'
script:
"""
bowtie2-build $host_fasta host_index
mkdir -p host_index_dir
mv *bt2 host_index_dir/
"""
}
process HOST_REMOVE {
input:
path(host_index_dir)
tuple val(sample_id), path(reads)
output:
tuple val(sample_id), path('*.fastq')
shell:
'''
index_prefix=$(ls !{host_index_dir} | head -n1 | sed -E "s/.[0-9]+.bt2//")
index_prefix_full=!{host_index_dir}/$index_prefix
bowtie2 \
-p !{task.cpus} \
-x $index_prefix_full \
-1 !{reads[0]} \
-2 !{reads[1]} \
--local \
--un-conc \
!{sample_id}_host_removed_reads \
> !{sample_id}_mapped_unmapped.sam
mv !{sample_id}_host_removed_reads.1 !{sample_id}_hostrm_R1.fastq
mv !{sample_id}_host_removed_reads.2 !{sample_id}_hostrm_R2.fastq
'''
}
process MAXBIN2 {
input:
tuple val(sample_id), path(assembly), path(reads)
output:
tuple val(sample_id), path ('*.fasta'), emit: fasta
script:
"""
run_MaxBin.pl \
-contig $assembly \
-reads ${reads[0]} \
-reads2 ${reads[1]} \
-out $sample_id
"""
}
process METABAT2 {
input:
tuple val(sample_id), path(assembly), path(bam), path(bam_idx), path(bed)
output:
path '*.fa', emit: fasta, optional: true
path 'depth.txt'
script:
"""
jgi_summarize_bam_contig_depths --outputDepth depth.txt "$bam"
metabat2 -i "$assembly" -a depth.txt -m 1500 --maxP 75 -s 100000 -o "$sample_id"
"""
}
process CONCOCT {
input:
tuple val(sample_id), path(assembly), path(bam), path(bam_idx), path(bed)
output:
path 'contigs10k.fasta'
path 'covtable.tsv'
path 'clustering_gt1000.csv'
path 'merged.csv'
path ('*.fa'), emit: fasta, optional: true
//path 'fasta_bins/'
script:
"""
cut_up_fasta.py "$assembly" -c 100000 --merge_last -b "$bed" > contigs10k.fasta
concoct_coverage_table.py "$bed" "$bam" > covtable.tsv
concoct --composition_file contigs10k.fasta --coverage_file covtable.tsv -s 100
merge_cutup_clustering.py clustering_gt1000.csv > merged.csv
mkdir -p fasta_bins
extract_fasta_bins.py "$assembly" merged.csv --output_path fasta_bins
"""
}
process DREP {
publishDir "${params.outdir}/drep", mode: "copy", pattern: "dereplicated_genomes"
input:
path fasta_dir
output:
path 'data_tables'
path 'dereplicated_genomes', emit: depreplicated_genomes
path 'data/checkM/checkM_outdir/results.tsv', emit: checkM_result
// flexible, probably best to specify what you want right here rather than down the line
script:
"""
dRep dereplicate \
'drep_out' \
-g "$fasta_dir/*.fa"
"""
}
process MAP2ASSEMBLY {
input:
tuple val(sample_id), path(reads)
tuple val(sample_id), path(assembly)
output:
tuple val(sample_id),
path("${sample_id}.bam"),
path("${sample_id}.bam.bai"),
path("${sample_id}.bed")
script:
"""
bwa index -p "$sample_id" "$assembly"
bwa mem -t $task.cpus -a "$sample_id" ${reads[0]} ${reads[1]} |
samtools sort -o "$sample_id".bam -
samtools index "$sample_id".bam
bedtools bamtobed -i "$sample_id".bam > "$sample_id".bed
"""
}