-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.nf
344 lines (241 loc) · 7.86 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
#!/usr/bin/env nextflow
import Helper
import CollectInitialMetadata
// Pipeline version
if (workflow.commitId){
version = "0.1 $workflow.revision"
} else {
version = "0.1 (local version)"
}
params.help = false
if (params.help){
Help.print_help(params)
exit 0
}
def infoMap = [:]
if (params.containsKey("fastq")){
infoMap.put("fastq", file(params.fastq).size())
}
if (params.containsKey("fasta")){
if (file(params.fasta) instanceof LinkedList){
infoMap.put("fasta", file(params.fasta).size())
} else {
infoMap.put("fasta", 1)
}
}
// checks if params.accessions is different from null
if (params.accessions) {
BufferedReader reader = new BufferedReader(new FileReader(params.accessions));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
infoMap.put("accessions", lines)
}
Help.start_info(infoMap, "$workflow.start", "$workflow.profile")
CollectInitialMetadata.print_metadata(workflow)
if (!params.accessions && !params.fastq){ exit 1, "'accessions' or 'fastq' parameter missing" }
if (params.accessions){
IN_accessions_raw = Channel.fromPath(params.accessions).ifEmpty { exit 1, "No accessions file provided with path:'${params.accessions}'" }
process fasterqDump {
tag { accession_id }
publishDir "reads", pattern: "${accession_id}/*fq.gz" // needed?
maxRetries 1 // adjust?
input:
val accession_id from IN_accessions_raw.splitText(){ it.trim() }.filter{ it.trim() != "" }
output:
set val({ "$name" != "null" ? "$name" : "$accession_id" }), file("${accession_id}/*fq") optional true into IN_fastq_raw
script:
"""
{
echo "Downloading the following accession: ${accession_id}"
fasterq-dump ${accession_id} -e ${task.cpus} -p
if [ ${params.compress_fastq} = true ]
then
echo "Compressing FastQ files..."
if [ -f ${accession_id}_1.fastq ]
then
pigz -p ${task.cpus} ${accession_id}_1.fastq ${accession_id}_2.fastq
elif [ -f ${accession_id}_3.fastq ]
then
echo "No paired end reads were found to compress."
pigz -p ${task.cpus} ${accession_id}_3.fastq
else
echo "FastQ files weren't compressed. Check if FastQ files were downloaded."
fi
elsenex
echo "FastQ files won't be compressed because compress_fastq options was set to: '${params.compress_fastq}.'"
fi
} || {
# If exit code other than 0
if [ \$? -eq 0 ]
then
echo "pass" > .status
else
echo "fail" > .status
echo "Could not download accession $accession_id" > .fail
fi
}
"""
}
} else {
if (!params.fastq){ exit 1, "'fastq' parameter missing"}
IN_fastq_raw = Channel.fromFilePairs(params.fastq).ifEmpty { exit 1, "No fastq files provided with pattern:'${params.fastq}'" }
}
process fastp {
tag {sample_id}
publishDir "results/fastp/", pattern: "*.html"
input:
set sample_id, file(fastq_pair) from IN_fastq_raw
output:
set sample_id, file('*_QC*') into OUT_fastq_QC
script:
"""
fastp -i ${fastq_pair[0]} -I ${fastq_pair[1]} -o ${sample_id}_QC_1.fq.gz -O ${sample_id}_QC_2.fq.gz -h ${sample_id}.html
"""
}
OUT_fastq_QC.into{ OUT_fastq_QC_1 ; OUT_fastq_QC_2 }
if (params.mode == "magicblast") {
IN_blastDB = Channel.fromPath("${params.blastdb}*")
process magicBLAST {
tag {sample_id}
input:
set sample_id, file(fastq_pair) from OUT_fastq_QC_1
file blastdb from IN_blastDB.collect()
output:
set sample_id, file('*_out.sam') into OUT_magicblast
script:
"""
echo \$(echo ${blastdb[0]} | sed -r 's/^(.*)\\.[a-z]+\$/\\1/') > blastdb_name.txt
magicblast -query ${fastq_pair[0]} -query_mate ${fastq_pair[1]} -infmt fastq -db \$(cat blastdb_name.txt) -outfmt sam -out ${sample_id}_out.sam -num_threads ${task.cpus} -paired -no_unaligned
"""
}
process samtools {
tag {sample_id}
input:
set sample_id, file(samfile) from OUT_magicblast
output:
set sample_id, file('*.depth') into OUT_samtools
script:
"""
samtools sort -o sorted.bam -O bam -@ ${task.cpus} ${samfile} && rm *.sam >> .command.log 2>&1
samtools depth sorted.bam > ${sample_id}.depth
"""
}
IN_reference = Channel.fromPath("${params.reference}")
process check_coverage {
tag {sample_id}
input:
set sample_id, file(depth_file) from OUT_samtools
each file(amr_reference) from IN_reference
output:
set sample_id, file("*.fasta") into OUT_baits
script:
template "magicBlast_depth_parser.pl"
}
} else if (params.mode == "mash") {
IN_reference = Channel.fromPath("${params.reference}")
IN_reference.into{ IN_reference_1 ; IN_reference_2 }
process mash_sketch {
tag {amr_reference}
storeDir 'mash_sketch/'
input:
file(amr_reference) from IN_reference_1
output:
file("*.msh") into OUT_mash_sketch
script:
"""
mash sketch -i ${amr_reference}
"""
}
process mash_screen {
tag {sample_id}
input:
set sample_id, file(fastq_pair) from OUT_fastq_QC_1
file(mash_sketch) from OUT_mash_sketch
output:
set sample_id, file("*.screen") into OUT_mash_screen
script:
"""
mash screen -p ${task.cpus} -w ${mash_sketch} ${fastq_pair} | awk '\$1>${params.mash_treshold}' > ${sample_id}.amr.screen
cut -f 5 ${sample_id}.amr.screen
"""
}
process mash_hits_compiler {
tag {sample_id}
input:
set sample_id, file(mash_results) from OUT_mash_screen
file(amr_reference) from IN_reference_2
output:
set sample_id, file("*.fasta") into OUT_baits
script:
template "mash_hits_compiler.py"
}
} else if (params.mode == "hmmer") {
process prepare_fastq {
tag {sample_id}
input:
set sample_id, file(fastq_pair) from OUT_fastq_QC_1
output:
set sample_id, file("*.fq") into OUT_preprare_fastq
script:
"""
pigz -dc ${fastq_pair} > ${sample_id}.fq
"""
}
process read_translator {
tag {sample_id}
input:
set sample_id, file(fastq) from OUT_preprare_fastq
output:
set sample_id, file("translated_reads.fa") into OUT_translator
script:
template "read_translator.py"
}
IN_hmmerDB = Channel.fromPath(params.hmmdb)
IN_nf_to_seq = Channel.fromPath(params.hmm_fasta)
IN_threshold = Channel.value(params.hmmr_threshold)
process hmmer {
tag {sample_id}
input:
set sample_id, file(translated_reads) from OUT_translator
file hmmerdb from IN_hmmerDB.collect()
val hmmr_threshold from IN_threshold
output:
set sample_id, file("hmmresult*") into OUT_hmmer
script:
"""
count=0
#echo ${hmmerdb}
for hmmfile in ${hmmerdb};
do
hmmsearch --cpu ${task.cpus} --noali -T ${hmmr_threshold} --acc --incT ${hmmr_threshold} -o "hmmresult_\$count" \$hmmfile ${translated_reads}
count=`expr \$count + 1`
done
"""
}
process hits_compiler{
tag {sample_id}
input:
set sample_id, file(hmmresult) from OUT_hmmer
file nf_to_seq from IN_nf_to_seq
output:
set sample_id, file("hit_nucleotide_seqs.fa") into OUT_baits
script:
template "hits_compiler.py"
}
} else {exit 1, "no recognized mode provided. available options: 'magiblast', 'mash', 'hmmer'"}
process guided_assembly {
tag {sample_id}
input:
set sample_id, file(baits), file(fastq_pairs) from OUT_baits.join(OUT_fastq_QC_2)
output:
set sample_id, file("*.ga.fa") into OUT_guided_assembly
script:
"""
mkdir temp
cp -L ${fastq_pairs} temp/
rm ${fastq_pairs} && mv temp/* . && rm -r temp
gunzip *.gz
guidedassembler --cores ${task.cpus} --fastq *_1.* --fastq *_2.* --targets ${baits} --contigs_out ${sample_id}.ga.fa
"""
}