-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnextflow_pipeline.nf
599 lines (414 loc) · 16.4 KB
/
nextflow_pipeline.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#!/usr/bin/env nextflow
genomes = Channel.fromFilePairs(params.path_to_reads).ifEmpty { error "Missing input reads, directory is either empty or missing read pair" }
genomes.into { raw_data_for_Fastp_trim; raw_data_for_Fastp_raw ; raw_data_for_trimmomatic; raw_data_for_spades; raw_data_for_bowtie2_spades_no_trim ; raw_data_for_bowtie2_skesa_no_trim ; raw_data_for_bowtie2_spades_fastp ; raw_data_for_bowtie2_skesa_fastp ; raw_data_for_bowtie2_spades_trimmomatic ; raw_data_for_bowtie2_skesa_trimmomatic ; raw_data_for_fastqc ; raw_data_for_skesa}
reference = Channel.value(params.path_to_reference).ifEmpty { error "No reference genome. Add reference genome to Raw_data directory" }
reference.into { ref_for_quast_no_trim ; ref_for_quast_fastp_trim ; ref_for_quast_trimmomatic_trim }
scripts_folder = Channel.value(params.path_to_scripts)
scripts_folder.into { script_fastqc ; script_spades_no_trim ; script_skesa_no_trim ; script_spades_fastp ; script_skesa_fastp ; script_spades_trimmomatic ; script_skesa_trimmomatic}
trimmomatic_setting = Channel.value(params.trimmomatic_set)
adapters = Channel.value(params.adapter)
minority_adapter = Channel.value(params.not_used_first_adapter)
spades_setting = Channel.value(params.spades_set)
spades_setting.into { no_trim_settings ; trim_trimmomatic_setting ; trim_fastp_setting }
filter_setting = Channel.value(params.filter_set)
filter_setting.into { no_trim_skesa_filter_setting ; fastp_skesa_filter_setting ; trimmomatic_skesa_filter_setting ; no_trim_spades_filter_setting ; fastp_spades_filter_setting ; trimmomatic_spades_filter_setting }
filter = params.filter_set
spades_set = params.spades_set.toUpperCase() - ~/--/
length = spades_set.length()
character = spades_set.substring(0, length - (length-1))
assembler = params.assembler
out_dir = ""
if ( params.no_trim == true )
out_dir = out_dir+"N"
if ( params.trimmomatic == true )
out_dir = out_dir+"T"
if ( params.fastp_trim_qc == true )
out_dir = out_dir+"F"
if ( params.assembler == 'skesa' )
out_dir = out_dir+"Ske"
if ( params.assembler == 'spades' )
out_dir = out_dir+"Sp"+character
if ( params.filter_contigs == true )
out_dir = out_dir+filter+"f"
if ( params.assembly_improvement == true )
out_dir = out_dir + "P"
//else
// out_dir = ""
// if ( params.no_trim == true )
// out_dir = out_dir+"NSp"
// if ( params.trimmomatic == true )
// out_dir = out_dir+"TSp"
// if ( params.fastp_trim_qc == true )
// out_dir = out_dir+"FSp"
// if ( params.filter_contigs == true )
// out_dir = out_dir+filter+"f"
// if ( params.assembly_improvement == true )
// out_dir = out_dir + "P"
// out_dir = out_dir+"_"+spades_set
//if ( params.no_trim == true )
// out_dir = out_dir+"No_trimming_"+assembler+"_"
//if ( params.trimmomatic == true )
// out_dir = out_dir+"After_trimmomatic_"+assembler+"_"
//if ( params.fastp_trim_qc == true )
// out_dir = out_dir+"After_fastp_"+assembler+"_"
//if ( params.assembler == 'spades' )
// out_dir = out_dir+spades_set+"_"
//if ( params.filter_contigs == true )
// out_dir = out_dir+filter+"filter_"
//if ( params.assembly_improvement == true )
// out_dir = out_dir+"Improved_pilon"
println { out_dir }
process fastp_trimming{
publishDir "./Results/${out_dir}/${sampleID}/Fastp/", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(reads) from raw_data_for_Fastp_trim
output:
tuple sampleID, file("${sampleID}*") into fastp_output
file "Reports/*" into fastp_reports
when:
params.fastp_trim_qc == true
"""
mkdir Reports
fastp -i ${reads[0]} -I ${reads[1]} -o ${sampleID}_R1_trimmed_fastp.fq.gz -O ${sampleID}_R2_trimmed_fastp.fq.gz
mv fastp* Reports/
"""
}
fastp_output.into { fastp_output_fastqc ; fastp_output_SPAdes ; fastp_output_skesa }
process fastqc_post_fastp_trim {
publishDir "./Results/${out_dir}/${sampleID}/FastQC_post_trim", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(reads) from fastp_output_fastqc
output:
file "*" into fastqc_trim_fastp
when:
params.fastp_trim_qc == true
"""
fastqc ${reads[0]} ${reads[1]} --outdir .
"""
}
process trimmomatic{
publishDir "./Results/${out_dir}/${sampleID}/Trimmomatic/", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(reads) from raw_data_for_trimmomatic
val settings from trimmomatic_setting
val adapter from adapters
val lab from minority_adapter
output:
tuple sampleID, file("*_paired.fq.gz") into trimmomatic_output
when:
params.trimmomatic == true
script:
if ( sampleID.contains(lab[0]) || sampleID.contains(lab[1]) )
adapters = adapter[1]
else
adapters = adapter[0]
"""
trimmomatic PE ${reads[0]} ${reads[1]} ${sampleID}_R1_paired.fq.gz ${sampleID}_R1_unpaired.fq.gz ${sampleID}_R2_paired.fq.gz ${sampleID}_R2_unpaired.fq.gz ${adapters} ${settings}
"""
}
trimmomatic_output.into { trimmomatic_output_for_SPAdes ; trimmomatic_output_for_FastQC ; trimmomatic_output_for_fastp ; trimmomatic_output_for_skesa}
process fastqc_post_trimmomatic {
publishDir "./Results/${out_dir}/${sampleID}/FastQC_post_trim", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(reads) from trimmomatic_output_for_FastQC
output:
file "*" into fastqc_raw
"""
fastqc ${reads[0]} ${reads[1]} --outdir .
"""
}
process fastp_qc_post_trimmomatic{
publishDir "./Results/${out_dir}/${sampleID}/Fastp_QC/", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(reads) from trimmomatic_output_for_fastp
output:
file "Reports/*" into fastp_reports_trimmomatic
when:
params.trimmomatic == true
"""
mkdir Reports
fastp -i ${reads[0]} -I ${reads[1]} -o ${sampleID}_R1_trimmed_fastp.fq.gz -O ${sampleID}_R2_trimmed_fastp.fq.gz
mv fastp* Reports/
"""
}
assembler = params.assembler
process assembly_no_trim{
publishDir "./Results/${out_dir}/${sampleID}/${assembler}", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(reads) from raw_data_for_spades
val settings from no_trim_settings
val script_folder from script_spades_no_trim
val filter_settings from no_trim_spades_filter_setting
output:
tuple sampleID, file("${sampleID}*") into spades_output
file "*" into spades_all
when:
params.no_trim == true
script:
if ( params.assembler == 'skesa' )
if ( params.filter_contigs == false )
"""
skesa --reads ${reads[0]},${reads[1]} --use_paired_ends > ${sampleID}_no_trim_skesa_contigs.fasta
"""
else
"""
skesa --reads ${reads[0]},${reads[1]} --use_paired_ends > ${sampleID}_no_trim_skesa_contigs.fasta
python3 ${script_folder}/remove_contaminants.py ${sampleID}_no_trim_skesa_contigs ${filter_settings}
rm ${sampleID}_no_trim_skesa_contigs.fasta
"""
else if ( params.assembler == 'spades')
if ( params.filter_contigs == false )
"""
spades.py -1 ${reads[0]} -2 ${reads[1]} -o . ${settings}
mv scaffolds.fasta ./${sampleID}_no_trim_spades_${settings}_scaffolds.fasta
"""
else
"""
spades.py -1 ${reads[0]} -2 ${reads[1]} -o . ${settings}
mv scaffolds.fasta ./${sampleID}_no_trim_spades_${settings}_scaffolds.fasta
python3 ${script_folder}/remove_contaminants_spades.py ${sampleID}_no_trim_spades_${settings}_scaffolds ${filter_settings}
rm ${sampleID}_no_trim_spades_${settings}_scaffolds.fasta
"""
}
process assembly_after_fastp{
publishDir "./Results/${out_dir}/${sampleID}/${assembler}", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(reads) from fastp_output_SPAdes
val script_folder from script_spades_fastp
val settings from trim_fastp_setting
val filter_settings from fastp_spades_filter_setting
output:
tuple sampleID, file("${sampleID}*") into spades_output_fastp
file "*" into spades_all_fastp
when:
params.fastp_trim_qc == true
script:
if ( params.assembler == 'skesa' )
if ( params.filter_contigs == false )
"""
skesa --reads ${reads[0]},${reads[1]} --use_paired_ends > ${sampleID}_trim_fastp_skesa_contigs.fasta
"""
else
"""
skesa --reads ${reads[0]},${reads[1]} --use_paired_ends > ${sampleID}_trim_fastp_skesa_contigs.fasta
python3 ${script_folder}/remove_contaminants.py ${sampleID}_trim_fastp_skesa_contigs ${filter_settings}
rm ${sampleID}_trim_fastp_skesa_contigs.fasta
"""
else if ( params.assembler == 'spades')
if ( params.filter_contigs == false )
"""
spades.py -1 ${reads[0]} -2 ${reads[1]} -o . ${settings}
mv scaffolds.fasta ./${sampleID}_trim_fastp_spades_${settings}_scaffolds.fasta
"""
else
"""
spades.py -1 ${reads[0]} -2 ${reads[1]} -o . ${settings}
mv scaffolds.fasta ./${sampleID}_trim_fastp_spades_${settings}_scaffolds.fasta
python3 ${script_folder}/remove_contaminants_spades.py ${sampleID}_trim_fastp_spades_${settings}_scaffolds ${filter_settings}
rm ${sampleID}_trim_fastp_spades_${settings}_scaffolds.fasta
"""
}
process assembly_after_trimmomatic{
publishDir "./Results/${out_dir}/${sampleID}/${assembler}", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(reads) from trimmomatic_output_for_SPAdes
val script_folder from script_spades_trimmomatic
val settings from trim_trimmomatic_setting
val filter_settings from trimmomatic_spades_filter_setting
output:
tuple sampleID, file("${sampleID}*") into spades_output_trimmomatic
file "*" into spades_all_trimmomatic
when:
params.trimmomatic == true
script:
if ( params.assembler == 'skesa' )
if ( params.filter_contigs == false )
"""
skesa --reads ${reads[0]},${reads[1]} --use_paired_ends > ${sampleID}_trim_trimmomatic_skesa_contigs.fasta
"""
else
"""
skesa --reads ${reads[0]},${reads[1]} --use_paired_ends > ${sampleID}_trim_trimmomatic_skesa_contigs.fasta
python3 ${script_folder}/remove_contaminants.py ${sampleID}_trim_trimmomatic_skesa_contigs ${filter_settings}
rm ${sampleID}_trim_trimmomatic_skesa_contigs.fasta
"""
else if ( params.assembler == 'spades')
if ( params.filter_contigs == false )
"""
spades.py -1 ${reads[0]} -2 ${reads[1]} -o . ${settings}
mv scaffolds.fasta ./${sampleID}_trim_trimmomatic_spades_${settings}_scaffolds.fasta
"""
else
"""
spades.py -1 ${reads[0]} -2 ${reads[1]} -o . ${settings}
mv scaffolds.fasta ./${sampleID}_trim_trimmomatic_spades_${settings}_scaffolds.fasta
python3 ${script_folder}/remove_contaminants_spades.py ${sampleID}_trim_trimmomatic_spades_${settings}_scaffolds ${filter_settings}
rm ${sampleID}_trim_trimmomatic_spades_${settings}_scaffolds.fasta
"""
}
spades_output.into { spades_output_for_quast_no_trim ; spades_output_for_pilon_no_trim ; spades_no_trim_sorting }
spades_output_fastp.into {spades_output_for_quast_fastp ; spades_output_fastp_for_pilon_trim ; spades_fastp_sorting }
spades_output_trimmomatic.into { spades_output_quast_trimmomatic ; spades_output_trimmomatic_for_pilon_trim ; spades_trimmomatic_sorting}
//join the raw data for bowtie with the spades results by using sampleID as key
spades_raw_data_join = spades_output_for_pilon_no_trim.join(raw_data_for_bowtie2_spades_no_trim)
spades_raw_trimmomatic_join = spades_output_trimmomatic_for_pilon_trim.join(raw_data_for_bowtie2_spades_trimmomatic)
spades_raw_fastp_join = spades_output_fastp_for_pilon_trim.join(raw_data_for_bowtie2_spades_fastp)
process pilon_post_spades_no_trim {
publishDir "./Results/${out_dir}/${sampleID}/Pilon/${assembler}", mode: 'copy'
input:
tuple sampleID, file(scaffold), file(reads) from spades_raw_data_join
output:
tuple sampleID, file("${sampleID}*") into pilon_output_spades_no_trim
when:
params.assembly_improvement==true && params.no_trim==true
script:
index_base = scaffold[0].toString() - ~/.fasta/
"""
mkdir index
bowtie2-build ${scaffold[0]} index/$index_base
bowtie2 -x index/${index_base} -1 ${reads[0]} -2 ${reads[1]} -p 4 | samtools sort -@ 4 -o ${sampleID}_sorted_alignment.bam
samtools index ${sampleID}_sorted_alignment.bam
pilon --genome ${scaffold[0]} --frags ${sampleID}_sorted_alignment.bam --output ${index_base}_improved
rm *.bam
rm *.bai
"""
}
process pilon_post_spades_fastp {
publishDir "./Results/${out_dir}/${sampleID}/Pilon/${assembler}", mode: 'copy'
input:
tuple sampleID, file(scaffold), file(reads) from spades_raw_fastp_join
output:
tuple sampleID, file("${sampleID}*") into pilon_output_spades_fastp
when:
params.assembly_improvement==true && params.fastp_trim_qc==true
script:
index_base = scaffold[0].toString() - ~/.fasta/
"""
mkdir index
bowtie2-build ${scaffold[0]} index/$index_base
bowtie2 -x index/${index_base} -1 ${reads[0]} -2 ${reads[1]} -p 4 | samtools sort -@ 4 -o ${sampleID}_sorted_alignment.bam
samtools index ${sampleID}_sorted_alignment.bam
pilon --genome ${scaffold[0]} --frags ${sampleID}_sorted_alignment.bam --output ${index_base}_improved
rm *.bam
rm *.bai
"""
}
process pilon_post_spades_trimmomatic {
publishDir "./Results/${out_dir}/${sampleID}/Pilon/${assembler}", mode: 'copy'
input:
tuple sampleID, file(scaffold), file(reads) from spades_raw_trimmomatic_join
output:
tuple sampleID, file("${sampleID}*") into pilon_output_spades_trimmomatic
when:
params.assembly_improvement==true && params.trimmomatic==true
script:
index_base = scaffold[0].toString() - ~/.fasta/
"""
mkdir index
bowtie2-build ${scaffold[0]} index/$index_base
bowtie2 -x index/${index_base} -1 ${reads[0]} -2 ${reads[1]} -p 4 | samtools sort -@ 4 -o ${sampleID}_sorted_alignment.bam
samtools index ${sampleID}_sorted_alignment.bam
pilon --genome ${scaffold[0]} --frags ${sampleID}_sorted_alignment.bam --output ${index_base}_improved
rm *.bam
rm *.bai
"""
}
process quast_no_trimming{
publishDir "./Results/${out_dir}/${sampleID}/Quast", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(assembly) from spades_output_for_quast_no_trim
val reference from ref_for_quast_no_trim
output:
file "${assembler}/*" into quast_output_spades_no_trim
file "Reports/*" into quast_report1
"""
mkdir Reports
quast.py ${assembly[0]} -o ${assembler} -r $reference
cp ${assembler}/report.tsv Reports/${sampleID}_${assembler}_${out_dir}.tsv
"""
}
process quast_after_fastp{
publishDir "./Results/${out_dir}/${sampleID}/Quast", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(assembly) from spades_output_for_quast_fastp
val reference from ref_for_quast_no_trim
output:
file "${assembler}/*" into quast_output_spades_fastp
file "Reports/*" into quast_report2
"""
mkdir Reports
quast.py ${assembly[0]} -o ${assembler} -r $reference
cp ${assembler}/report.tsv Reports/${sampleID}_${assembler}_${out_dir}.tsv
"""
}
process quast_after_trimmomatic{
publishDir "./Results/${out_dir}/${sampleID}/Quast", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(assembly) from spades_output_quast_trimmomatic
val reference from ref_for_quast_no_trim
output:
file "${assembler}/*" into quast_output_spades_trimmomatic
file "Reports/*" into quast_report3
"""
mkdir Reports
quast.py ${assembly[0]} -o ${assembler} -r $reference
cp ${assembler}/report.tsv Reports/${sampleID}_${assembler}_${out_dir}.tsv
"""
}
process quast_no_trimming_improved{
publishDir "./Results/${out_dir}/${sampleID}/Quast", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(assembly) from pilon_output_spades_no_trim
val reference from ref_for_quast_no_trim
output:
file "${assembler}/*" into quast_output_spades_improved_no_trim
file "Reports/*" into quast_report4
"""
mkdir Reports
quast.py ${assembly[0]} -o ${assembler} -r $reference
cp ${assembler}/report.tsv Reports/${sampleID}_${assembler}_${out_dir}.tsv
"""
}
process quast_after_fastp_improved{
publishDir "./Results/${out_dir}/${sampleID}/Quast", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(assembly) from pilon_output_spades_fastp
val reference from ref_for_quast_no_trim
output:
file "${assembler}/*" into quast_output_spades_improved_fastp
file "Reports/*" into quast_report5
"""
mkdir Reports
quast.py ${assembly[0]} -o ${assembler} -r $reference
cp ${assembler}/report.tsv Reports/${sampleID}_${assembler}_${out_dir}.tsv
"""
}
process quast_after_trimmomatic_improved{
publishDir "./Results/${out_dir}/${sampleID}/Quast", mode: 'copy'
tag "${sampleID}"
input:
tuple sampleID, file(assembly) from pilon_output_spades_trimmomatic
val reference from ref_for_quast_no_trim
output:
file "${assembler}/*" into quast_output_improved_trimmomatic
file "Reports/*" into quast_report6
"""
mkdir Reports
quast.py ${assembly[0]} -o ${assembler} -r $reference
cp ${assembler}/report.tsv Reports/${sampleID}_${assembler}_${out_dir}.tsv
"""
}
//result.view { it }