-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnf-assembly.nf
448 lines (353 loc) · 14.3 KB
/
nf-assembly.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
#!/home/johannes/bin nextflow
VERSION = "1.2.0"
//+++++++++++++++++ SETUP++++++++++++++++++++++++
// setup paths to programs
params.trimmomatic = "/opt/trimmomatic/current/trimmomatic*.jar"
params.spades = "/opt/spades/current/bin/spades.py"
params.genemark = "/opt/genemark-ES/gmes_petap.pl"
params.infernal_cmpress = "cmpress"
params.infernal_cmscan = "cmscan"
params.trnascan = "tRNAscan-SE"
params.quast = "/opt/quast/current/quast.py"
// point this to the scripts directory of this repository
params.scripts = "/home/johannes/rdrive/Johannes-DEBLEJ-SE00276/bioinformatics/nf-assembly/scripts"
// Path to raw fastq.gz files
// This pipeline can be run either targeted at individual isolates
// or untargeted if you want to assemble and annotate everything
// within the input folder
// --> uncomment this for individual isolates
// ID of isolate you want to assemble
params.isolate = "P94-24"
params.input = "/home/johannes/rdrive/PPG_SEQ_DATA-LICHTJ-SE00182/Basespace/**/LinaSample*/${params.isolate}*_R{1,2}_*fastq.gz"
// --> uncomment this for all isolates in input folder
// params.input = "/home/johannes/rdrive/PPG_SEQ_DATA-LICHTJ-SE00182/Basespace/**/LinaSample*/*_R{1,2}_*fastq.gz"
// Path where output data shall go
params.outputdir = "/home/johannes/rdrive/PPG_SEQ_DATA-LICHTJ-SE00182/johannes/notebook/test"
//+++++++++++++++++++++++++++++++++++++++++++++++
// Create channel that provides the sampleID and the raw read files
reads = Channel
.fromFilePairs(params.input)
.map {sampleID, fwdrevreads -> [sampleID.tokenize('_')[0], fwdrevreads]}
log.info "=============================================================================="
log.info "Illumina assembly, RNA annotation and Genemark prediction version " + VERSION
log.info "Isolate : ${params.isolate}"
log.info "Output : ${params.outputdir}/${params.isolate}"
log.info "=============================================================================="
reads
.groupTuple()
.map {sampleID, ary -> [sampleID, ary.transpose()]}
.map {sampleID, ary -> [sampleID, ary[0], ary[1]]}
.set {rawReads}
// Create a file that contains the version numbers of the tools run at the time
// the pipeline was run
process versions {
tag {sampleID}
publishDir "${params.outputdir}/${params.isolate}", mode: 'copy'
output:
file('versions.txt')
"""
echo "Programs and versions used in this pipeline:" >> versions.txt
date >> versions.txt
echo "============================================" >> versions.txt
echo "trimmomatic:" >> versions.txt
java -jar ${params.trimmomatic} -version >> versions.txt
echo "--------------------------------------------" >> versions.txt
echo "spades:" >> versions.txt
${params.spades} -v >> versions.txt
echo "--------------------------------------------" >> versions.txt
echo "GeneMarkES:" >> versions.txt
${params.genemark} | grep 'GeneMark-ES Suite version' >> versions.txt
echo "--------------------------------------------" >> versions.txt
echo "infernal:" >> versions.txt
${params.infernal_cmpress} -h | grep INFERNAL >> versions.txt
echo "--------------------------------------------" >> versions.txt
echo "trnaScanSE:" >> versions.txt
${params.trnascan} -h 2>&1 | head -2 | grep tRNAscan >> versions.txt
echo "--------------------------------------------" >> versions.txt
echo "Quast:" >> versions.txt
${params.quast} -v >> versions.txt
"""
}
// Combine individual read files into one for forward and one for reverse reads
process combineReads {
tag {sampleID}
publishDir "${params.outputdir}/${sampleID}/00-rawReads/", mode: 'copy'
input:
set sampleID, "fwd.*.fastq.gz", "rev.*.fastq.gz" from rawReads
output:
set sampleID, "${sampleID}.fwd.fastq.gz", "${sampleID}.rev.fastq.gz" into rawCombinedReads
"""
cat fwd.*.fastq.gz > ${sampleID}.fwd.fastq.gz
cat rev.*.fastq.gz > ${sampleID}.rev.fastq.gz \
"""
}
// Quality trimming with Trimmomatic
process trimReadsWithTrimmomatic {
tag {sampleID}
cpus 5
publishDir "${params.outputdir}/${sampleID}/01-trimmedReads/trimmomatic/", mode: 'copy'
input:
set sampleID, "fwd.fastq.gz", "rev.fastq.gz" from rawCombinedReads
output:
set sampleID, "${sampleID}.trimmed.fwd.fastq.gz", "${sampleID}.trimmed.rev.fastq.gz", "${sampleID}.singletons.fastq.gz" into trimmedReads
"""
cp /opt/trimmomatic/current/adapters/NexteraPE-PE.fa .
java -jar ${params.trimmomatic} PE \
-threads ${task.cpus} \
fwd.fastq.gz rev.fastq.gz \
${sampleID}.trimmed.fwd.fastq.gz singles.fwd.fastq.gz \
${sampleID}.trimmed.rev.fastq.gz singles.rev.fastq.gz \
ILLUMINACLIP:NexteraPE-PE.fa:2:30:10 SLIDINGWINDOW:4:20 MINLEN:50
cat singles.*.fastq.gz > ${sampleID}.singletons.fastq.gz
"""
}
// SPAdes short read assembly from Trimmomatic trimmed reads
// Since Trimmomatic only trims the reads, we want SPAdes to perfom
// some quality filtering. It does this by default
process assemblySpades {
tag {sampleID}
memory '25G'
cpus 10
input:
set sampleID, "paired.fwd.fastq.gz", "paired.rev.fastq.gz", "singletons.fastq.gz" from trimmedReads
output:
set sampleID, "${sampleID}.contigs.fasta" into contigsForCleanup
set sampleID, "${sampleID}.scaffolds.fasta" into scaffoldsForCleanup
"""
${params.spades} \
-k 21,33,55,77 \
--memory ${task.memory.toGiga()} \
--threads ${task.cpus} \
--careful \
-1 paired.fwd.fastq.gz \
-2 paired.rev.fastq.gz \
-s singletons.fastq.gz \
-o .
mv scaffolds.fasta ${sampleID}.scaffolds.fasta
mv contigs.fasta ${sampleID}.contigs.fasta
"""
}
process cleanupSpadesOutputScaffolds {
tag {sampleID}
input:
set sampleID, "${sampleID}.scaffolds.fasta" from scaffoldsForCleanup
output:
set sampleID, "${sampleID}.scaffolds.clean.fasta" into scaffoldsForHeaderadjustment
"""
#!/usr/bin/env ruby
require 'optparse'
require 'bio'
require 'pp'
options = {:prefix => nil}
OptionParser.new do |opts|
opts.banner = "Usage: fix_names.rb [options] input.fasta"
opts.on("-p", "--prefix [NAME]", "Strain prefix to prepend to contig name") do |p|
options[:prefix] = p + "_"
end
end.parse!
out = File.open("${sampleID}.scaffolds.clean.fasta", 'w')
Bio::FlatFile
.open("${sampleID}.scaffolds.fasta")
.sort_by{|entry| entry.length}
.reverse
.each
.with_index(1)
.map{|entry, i| [entry, "%sscf%d" % [options[:prefix], i]]}
.each{|entry, name| out.puts entry.seq.to_fasta(name, 80)}
out.close
"""
}
process cleanupSpadesOutputContigs {
tag {sampleID}
input:
set sampleID, "${sampleID}.contigs.fasta" from contigsForCleanup
output:
set sampleID, "${sampleID}.contigs.clean.fasta" into contigsForHeaderadjustment
"""
#!/usr/bin/env ruby
require 'optparse'
require 'bio'
require 'pp'
options = {:prefix => nil}
OptionParser.new do |opts|
opts.banner = "Usage: fix_names.rb [options] input.fasta"
opts.on("-p", "--prefix [NAME]", "Strain prefix to prepend to contig name") do |p|
options[:prefix] = p + "_"
end
end.parse!
out = File.open("${sampleID}.contigs.clean.fasta", 'w')
Bio::FlatFile
.open("${sampleID}.contigs.fasta")
.sort_by{|entry| entry.length}
.reverse
.each
.with_index(1)
.map{|entry, i| [entry, "%sctg%d" % [options[:prefix], i]]}
.each{|entry, name| out.puts entry.seq.to_fasta(name, 80)}
out.close
"""
}
process addSpeciesNameToFastaHeadersScaffolds {
tag {sampleID}
publishDir "${params.outputdir}/${sampleID}/02-assembly/spades/", mode: 'copy'
input:
set sampleID, "${sampleID}.scaffolds.clean.fasta" from scaffoldsForHeaderadjustment
output:
set sampleID, "${sampleID}.scaffolds.clean.fasta" into scaffoldsRawForGenemark
set sampleID, "${sampleID}.scaffolds.clean.fasta" into scaffoldsRawForTRNAscan
set sampleID, "${sampleID}.scaffolds.clean.fasta" into scaffoldsRawForInfernal
set sampleID, "${sampleID}.scaffolds.clean.fasta" into scaffoldsRawForQuast
set sampleID, "${sampleID}.scaffolds.clean.fasta" into scaffoldsRawForFastQC
"""
sed 's,>,&${sampleID}_,g' -i ${sampleID}.scaffolds.clean.fasta
"""
}
process addSpeciesNameToFastaHeadersContigs {
tag {sampleID}
publishDir "${params.outputdir}/${sampleID}/02-assembly/spades/", mode: 'copy'
input:
set sampleID, "${sampleID}.contigs.clean.fasta" from contigsForHeaderadjustment
output:
set sampleID, "${sampleID}.contigs.clean.fasta" into contigsRawForGenemark
set sampleID, "${sampleID}.contigs.clean.fasta" into contigsRawForTRNAscan
set sampleID, "${sampleID}.contigs.clean.fasta" into contigsRawForInfernal
set sampleID, "${sampleID}.contigs.clean.fasta" into contigsRawForFastQC
set sampleID, "${sampleID}.contigs.clean.fasta" into contigsRawForQuast
"""
sed 's,>,&${sampleID}_,g' -i ${sampleID}.contigs.clean.fasta
"""
}
// de novo gene annotation with GenemarkES
process annotation_genemark_scaffolds {
tag {sampleID}
cpus 10
publishDir "${params.outputdir}/${sampleID}/03-annotation/", mode: 'copy'
input:
set sampleID, "${sampleID}.scaffolds.clean.fasta" from scaffoldsRawForGenemark
output:
set sampleID, "${sampleID}.scaffolds.genemark.gtf"
"""
${params.genemark} --ES --fungus --cores ${task.cpus} --sequence ${sampleID}.scaffolds.clean.fasta
mv genemark.gtf ${sampleID}.scaffolds.genemark.gtf
"""
}
process annotation_genemark_contigs {
tag {sampleID}
cpus 10
publishDir "${params.outputdir}/${sampleID}/03-annotation/", mode: 'copy'
input:
set sampleID, "${sampleID}.contigs.clean.fasta" from contigsRawForGenemark
output:
set sampleID, "${sampleID}.contigs.genemark.gtf"
"""
/opt/genemark-ES/gmes_petap.pl --ES --fungus --cores ${task.cpus} --sequence ${sampleID}.contigs.clean.fasta
mv genemark.gtf ${sampleID}.contigs.genemark.gtf
"""
}
// tRNA annotation with tRNAscanSE
process annotation_trnascan_scaffolds {
tag {sampleID}
publishDir "${params.outputdir}/${sampleID}/03-annotation/", mode: 'copy'
input:
set sampleID, "${sampleID}.scaffolds.clean.fasta" from scaffoldsRawForTRNAscan
output:
set sampleID, "${sampleID}.scaffolds.trnascanSE.gff3"
"""
${params.trnascan} -o trnascanoutput.out ${sampleID}.scaffolds.clean.fasta
${params.scripts}/convert_tRNAScanSE_to_gff3.pl --input=trnascanoutput.out > ${sampleID}.scaffolds.trnascanSE.gff3
"""
}
process annotation_trnascan_contigs {
tag {sampleID}
publishDir "${params.outputdir}/${sampleID}/03-annotation/", mode: 'copy'
input:
set sampleID, "${sampleID}.contigs.clean.fasta" from contigsRawForTRNAscan
output:
set sampleID, "${sampleID}.contigs.trnascanSE.gff3"
"""
${params.trnascan} -o trnascanoutput.out ${sampleID}.contigs.clean.fasta
${params.scripts}/convert_tRNAScanSE_to_gff3.pl --input=trnascanoutput.out > ${sampleID}.contigs.trnascanSE.gff3
"""
}
// RNA annotation with infernal
process annotaton_infernal_scaffolds {
tag {sampleID}
cpus 10
input:
set sampleID, "${sampleID}.scaffolds.clean.fasta" from scaffoldsRawForInfernal
output:
set sampleID, "scaffolds.cmscan.tbl" into infernalToGff3scaffolds
"""
wget ftp://ftp.ebi.ac.uk/pub/databases/Rfam/14.1/Rfam.clanin
wget ftp://ftp.ebi.ac.uk/pub/databases/Rfam/14.1/Rfam.cm.gz
gzip -d Rfam.cm.gz
${params.infernal_cmpress} Rfam.cm
${params.infernal_cmscan} --rfam --cpu ${task.cpus} --cut_ga --nohmmonly --tblout scaffolds.cmscan.tbl --fmt 2 --clanin Rfam.clanin Rfam.cm ${sampleID}.scaffolds.clean.fasta > infernal.cmscan
"""
}
process annotaton_infernal_contigs {
tag {sampleID}
cpus 10
input:
set sampleID, "${sampleID}.contigs.clean.fasta" from contigsRawForInfernal
output:
set sampleID, "contigs.cmscan.tbl" into infernalToGff3contigs
"""
wget ftp://ftp.ebi.ac.uk/pub/databases/Rfam/13.0/Rfam.clanin
wget ftp://ftp.ebi.ac.uk/pub/databases/Rfam/13.0/Rfam.cm.gz
gzip -d Rfam.cm.gz
${params.infernal_cmpress} Rfam.cm
${params.infernal_cmscan} --rfam --cpu ${task.cpus} --cut_ga --nohmmonly --tblout contigs.cmscan.tbl --fmt 2 --clanin Rfam.clanin Rfam.cm ${sampleID}.contigs.clean.fasta > infernal.cmscan
"""
}
// infernal output conversion to GFF3
process infernalToGff3scaffolds {
tag {sampleID}
publishDir "${params.outputdir}/${sampleID}/03-annotation/", mode: 'copy'
input:
set sampleID, "scaffolds.cmscan.tbl" from infernalToGff3scaffolds
output:
set sampleID, "${sampleID}.scaffolds.infernal.gff3"
"""
grep -v ^# scaffolds.cmscan.tbl > scaffolds.cmscan.clean.tbl && awk '{printf "%s\tinfernal\t%s\t%d\t%d\t%s\t%s\t.\tNote=RfamID-%s\\n" ,\$4,\$2,\$10,\$11,\$17,\$12,\$3}' scaffolds.cmscan.clean.tbl > ${sampleID}.scaffolds.infernal.gff3
"""
}
process infernalToGff3contigs {
tag {sampleID}
publishDir "${params.outputdir}/${sampleID}/03-annotation/", mode: 'copy'
input:
set sampleID, "contigs.cmscan.tbl" from infernalToGff3contigs
output:
set sampleID, "${sampleID}.contigs.infernal.gff3"
"""
grep -v ^# contigs.cmscan.tbl > contigs.cmscan.clean.tbl && awk '{printf "%s\tinfernal\t%s\t%d\t%d\t%s\t%s\t.\tNote=RfamID-%s\\n" ,\$4,\$2,\$10,\$11,\$17,\$12,\$3}' contigs.cmscan.clean.tbl > ${sampleID}.contigs.infernal.gff3
"""
}
process quastScaffolds {
tag {sampleID}
publishDir "${params.outputdir}/${sampleID}/04-QC/scaffolds/", mode: 'copy'
input:
set sampleID, "${sampleID}.fasta" from scaffoldsRawForQuast
output:
file("quast_results/*") into quastResultsScaffolds
"""
${params.quast} --fungus --conserved-genes-finding --min-contig 0 --threads 10 --split-scaffolds ${sampleID}.fasta
"""
}
process quastContigs {
tag {sampleID}
publishDir "${params.outputdir}/${sampleID}/04-QC/contigs/", mode: 'copy'
input:
set sampleID, "${sampleID}.fasta" from contigsRawForQuast
output:
file("quast_results/*") into quastResultsContigs
"""
${params.quast} --fungus --conserved-genes-finding --min-contig 0 --threads 10 ${sampleID}.fasta
"""
}
workflow.onComplete {
log.info "========================================================"
log.info "Pipeline completed at: $workflow.complete"
log.info "Execution status: ${ workflow.success ? 'OK' : 'Failed' }"
log.info "========================================================"
}