-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.nf
634 lines (450 loc) · 14.9 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
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// Author: Noah Austin Legall
input = ""
output = ""
run_mode = "snp"
params.version = false
if(params.version){
println("v0.1")
exit(0)
}
params.help = false
if(params.help){
println(
"""
M B O V P A N (v1)
=============================
A pangenomic pipeline for the analysis of
Mycobacterium bovis isolates
usage: nextflow run mbovpan/mbovpan.nf [options] --input ./path/to/input --output ./path/to/output
options:
--run [all|snp|pan]:
Specifies in what mode to run mbovpan in [DEFAULT:all]
--qual [INT]:
The minimum QUAL score for a SNP to be considered [DEFAULT:20]
--depth [INT]:
The minimum DP score for a SNP to be considered [DEFAULT:25]
--mapq [INT]:
The minimum MQ score for a SNP to be considered [DEFAULT:40]
--threads [INT]:
How many threads to use for the programs [DEFAULT:(number of avail. threads)/2]
--help
Prints this help message
--version
Prints the current version
=============================
"""
)
exit(0)
}
// Automatically uses all the cpus that are available
// If not specified, use 50% of available resources
params.threads = Math.floor(Runtime.getRuntime().availableProcessors()/2)
reads = ""
// inital values based on previous benchmark work from dissertation
// can be changed by user
params.qual = 20
params.depth = 25
params.mapq = 40
if(params.qual){
qual = params.qual as Integer
}
if(params.depth){
depth = params.depth as Integer
}
if(params.mapq){
mapq = params.mapq as Integer
}
// record the path for key files in the repo
ref = "$workflow.projectDir/ref/mbovAF212297_reference.fasta" // reference genome
range = "$workflow.projectDir/chrom_ranges.txt" // for freebayes-parallel
spotyping = "$workflow.projectDir/scripts/SpoTyping/SpoTyping.py" //spoligotyping
check = "$workflow.projectDir/scripts/lineage_check.py"
lineage_table = "$workflow.projectDir/scripts/lineage_table.py"
// are default parameters included?
if(params.input == null || params.output == null){
println "necessary paramaters aren't supplied - supply values for input and/or output"
exit 0
}
else {
println "necessary paramaters supplied"
input = params.input
output = params.output
}
// what part of the pipeline should be ran?
if(params.run == "all" ){
println "mbovpan will infer snps and pangenome"
run_mode = "all"
}
else if(params.run == "snp"){
println "mbovpan will only infer snps"
run_mode = "snp"
}
else if(params.run == "pan"){
println "mbovpan will only infer the pangenome"
run_mode = "pan"
}
else {
println "mbovpan will infer both snps and pangenome by default"
run_mode = "all"
}
// how many threads will be utilized
if(params.threads){
println "mbovpan will run using ${params.threads} threads"
threads = params.threads
}
else{
println "mbovpan will run using ${threads} threads by default"
}
// finding the reads from the input file location
reads = Channel.fromFilePairs("$input*{1,2}*.f*q*").ifEmpty { error "Cannot find the read files" }
println("""
M B O V P A N (v1.0.0)
=============================
A pangenomic pipeline for the analysis of
Mycobacterium bovis isolates
Project : $workflow.projectDir
Git info: $workflow.repository - $workflow.revision [$workflow.commitId]
Cmd line: $workflow.commandLine
Manifest's pipeline version: $workflow.manifest.version
=============================
Summary of pipeline run
run type: $run_mode
reference location: $ref
input: $input
output: $output
no. of threads: $threads
QUAL: $qual
MAPQ: $mapq
DEPTH: $depth
=====================================
""")
// Start of the nextflow processes
process spotyping {
publishDir = "$output/mbovpan_results/spotyping"
conda "$workflow.projectDir/envs/spotyping.yaml"
errorStrategy 'ignore'
debug true
input:
tuple sample_id, file(reads_file) from reads
output:
tuple file("${reads_file[0].baseName}.fastq"), file("${reads_file[1].baseName}.fastq") into spoligo_ch
file("${reads_file[0].baseName - ~/_1*/}.out") into spoligo_multi
script:
"""
python3 ${spotyping} ${reads_file[0]} ${reads_file[1]} -o ${reads_file[0].baseName - ~/_1*/}.out > stdout.txt
python3 ${check}
"""
}
spoligo_ch.into {
spoligo_pre
spoligo_post
spoligo_process
}
process pre_fastqc {
conda 'bioconda::fastqc'
publishDir = "$output/mbovpan_results/fastqc"
input:
tuple file(read_one), file(read_two) from spoligo_pre
output:
file("pre_fastqc_${read_one.baseName - ~/_1*/}_logs") into fastqc_ch1
script:
"""
mkdir pre_fastqc_${read_one.baseName - ~/_1*/}_logs
fastqc -o pre_fastqc_${read_one.baseName - ~/_1*/}_logs -f fastq -q ${read_one} ${read_two}
"""
}
process fastp {
conda 'bioconda::fastp'
publishDir = "$output/mbovpan_results/read_trimming"
cpus threads
input:
tuple file(read_one), file(read_two) from spoligo_process
output:
file("${read_one.baseName - ~/_1/}_trimmed_R*.fastq") into fastp_ch
script:
"""
fastp -w ${task.cpus} -q 30 --detect_adapter_for_pe -i ${read_one} -I ${read_two} -o ${read_one.baseName - ~/_1/}_trimmed_R1.fastq -O ${read_one.baseName - ~/_1/}_trimmed_R2.fastq
"""
}
fastp_ch.into {
fastp_reads1
fastp_reads2
fastp_reads3
fastp_reads4
fastp_reads5
fastp_reads_lineage
}
process post_fastqc {
conda 'bioconda::fastqc'
publishDir = "$output/mbovpan_results/fastqc"
input:
tuple file(trim1), file(trim2) from spoligo_post
output:
file("post_fastqc_${trim1.baseName - ~/_1*/}_logs") into fastqc_ch2
script:
"""
mkdir post_fastqc_${trim1.baseName - ~/_1/}_logs
fastqc -o post_fastqc_${trim1.baseName - ~/_1/}_logs -f fastq -q ${trim1} ${trim2}
"""
}
process lineage {
publishDir = "$output/mbovpan_results/lineage"
conda "$workflow.projectDir/envs/tbprofile.yaml"
input:
tuple file(trim1), file(trim2) from fastp_reads_lineage
output:
file("${trim1.baseName - ~/_trimmed_R*/}.results.json") into tbprofile_ch
script:
"""
tb-profiler profile --read1 ${trim1} --read2 ${trim2} --no_delly --prefix ${trim1.baseName - ~/_trimmed_R*/}
cp ./results/${trim1.baseName - ~/_trimmed_R*/}.results.json ./
"""
}
bam = Channel.create()
if(run_mode == "snp" || run_mode == "all"){
process read_map {
publishDir = output
cpus threads
conda "samtools bioconda::bowtie2=2.4.2"
input:
tuple file(trim1), file (trim2) from fastp_reads2
output:
file("${trim1.baseName - ~/_trimmed_R*/}.bam") into map_ch
script:
"""
bowtie2 --threads ${task.cpus} -x $workflow.projectDir/ref/mbov_bowtie_index -1 ${trim1} -2 ${trim2} | samtools view -Sb | samtools sort -o ${trim1.baseName - ~/_trimmed_R*/}.bam
"""
}
bam = map_ch
process mark_dups {
publishDir = "$output/mbovpan_results/readmapping"
conda "$workflow.projectDir/envs/picard.yaml"
input:
file(bam) from bam
output:
file("${bam.baseName}.nodup.bam") into nodup_ch
script:
"""
picard MarkDuplicates INPUT=${bam} OUTPUT=${bam.baseName}.nodup.bam ASSUME_SORTED=true REMOVE_DUPLICATES=true METRICS_FILE=dup_metrics.csv USE_JDK_DEFLATER=true USE_JDK_INFLATER=true
samtools index ${bam.baseName}.nodup.bam
cp ${bam.baseName}.nodup.bam.bai $workflow.launchDir
"""
}
nodup_ch.into {
nodup1_ch
nodup2_ch
}
process freebayes {
publishDir = "$output/mbovpan_results/variant_calling"
cpus threads
conda "$workflow.projectDir/envs/freebayes.yaml"
input:
file(bam) from nodup1_ch
path(reference) from ref
output:
file("${bam.baseName - ~/.nodup/}.vcf") into freebayes_ch
script:
"""
cp $workflow.launchDir/${bam.baseName - ~/.nodup/}.nodup.bam.bai ./
freebayes-parallel ${range} ${task.cpus} -f ${ref} ${bam} > ${bam.baseName - ~/.nodup/}.vcf
"""
}
process vcf_filter {
publishDir = "$output/mbovpan_results/variant_calling"
conda "$workflow.projectDir/envs/vcflib.yaml"
debug true
input:
file(vcf) from freebayes_ch
output:
file("${vcf.baseName}.filtered.vcf") into filter_ch
script:
"""
vcffilter -f "QUAL > ${qual}" ${vcf} | vcffilter -f "DP > ${depth}" | vcffilter -f "MQM > ${mapq}" | vcffilter -f "TYPE = snp" | bedtools intersect -header -a - -b $workflow.projectDir/ref/pe_ppe_regions.gff3 -v > ${vcf.baseName}.filtered.vcf
"""
}
filter_ch.into {
filter1_ch
filter2_ch
}
stats_ch = fastp_reads4.merge(nodup2_ch).merge(filter2_ch)
process psuedo_assembly {
publishDir = "$output/mbovpan_results/assemblies"
conda "$workflow.projectDir/envs/consensus.yaml"
input:
file(vcf) from filter1_ch
output:
file("${vcf.baseName - ~/.filtered/}.consensus.fasta") into fasta_ch
script:
"""
bgzip ${vcf}
bcftools index ${vcf}.gz
bcftools norm --check-ref s --fasta-ref $ref -Ov ${vcf}.gz > ${vcf.baseName}.norm.vcf
bgzip ${vcf.baseName}.norm.vcf
bcftools index ${vcf.baseName}.norm.vcf.gz
cat ${ref} | vcf-consensus ${vcf.baseName}.norm.vcf.gz > ${vcf.baseName - ~/.filtered/}.dummy.fasta
sed 's|LT708304.1|${vcf.baseName - ~/.filtered/}}|g' ${vcf.baseName - ~/.filtered/}.dummy.fasta > ${vcf.baseName - ~/.filtered/}.consensus.fasta
"""
}
process iqtree_phylo {
publishDir = "$output/mbovpan_results/phylogeny"
conda "$workflow.projectDir/envs/iqtree.yaml"
errorStrategy "ignore"
cpus threads
input:
file(aln) from fasta_ch.collect()
output:
file("*") into phylo_ch
file("phylo_out.txt") into statistics_ch
script:
"""
cat *.fasta > mbovpan_align.fasta
cat $workflow.projectDir/ref/mbov_reference.fasta >> mbovpan_align.fasta
snp-sites -o mbovpan_align.snp_only.fasta mbovpan_align.fasta
iqtree -s mbovpan_align.snp_only.fasta -m MFP -nt ${task.cpus} -bb 1000 -pre mbovpan_align -o "LT708304.1"
echo "file created to make statistics file build conda env last" > phylo_out.txt
"""
}
process stats {
publishDir = "$output/mbovpan_results/statistics"
conda "$workflow.projectDir/envs/statistics.yaml"
input:
file(nec_files) from stats_ch
file(dummy_variable) from statistics_ch
output:
file("${nec_files[0].baseName}.stats") into output_stat_ch
script:
"""
python $workflow.projectDir/scripts/statistics.py ${nec_files[0]} ${nec_files[1]} ${nec_files[2]} ${nec_files[3]} > ${nec_files[0].baseName}.stats
"""
}
}
assembly = Channel.create()
if(run_mode == "pan" || run_mode == "all"){
process assembly {
publishDir = output
conda "bioconda::spades"
errorStrategy "ignore"
cpus threads
input:
tuple file(trim1), file(trim2) from fastp_reads3
output:
file("${trim1.baseName - ~/_trimmed_R*/}.scaffold.fasta") into assembly_ch
script:
"""
mkdir ${trim1.baseName}
spades.py -1 ${trim1} -2 ${trim2} --careful -o ${trim1.baseName} -t ${task.cpus} --only-assembler
cd ${trim1.baseName}
mv scaffolds.fasta ../${trim1.baseName - ~/_trimmed_R*/}.scaffold.fasta
"""
}
assembly_ch = assembly_ch
assembly_ch.into {
assembly_ch1
assembly_ch2
}
process quast {
publishDir = "$output/mbovpan_results/statistics"
conda "$workflow.projectDir/envs/quast.yaml"
cpus threads
input:
file(assemblies) from assembly_ch1.collect()
output:
file("*") into quast_ch
script:
"""
quast -o assembly_stats ${assemblies} -r ${ref} -t ${task.cpus}
"""
}
process annotate {
publishDir = "$output/mbovpan_results/annotations"
cpus threads
conda "$workflow.projectDir/envs/prokka.yaml"
errorStrategy "ignore"
input:
file(assembly) from assembly_ch2
output:
file("${assembly.baseName - ~/.scaffold/}.annot.gff") into annotate_ch
script:
"""
prokka --outdir ./${assembly.baseName - ~/.scaffold/} --cpus ${task.cpus} --prefix ${assembly.baseName - ~/.scaffold/}.annot ${assembly}
cp ./${assembly.baseName - ~/.scaffold/}/${assembly.baseName - ~/.scaffold/}.annot.gff ./
"""
}
process panaroo {
publishDir = "$output/mbovpan_results/pangenome"
cpus threads
input:
file(gff) from annotate_ch.collect()
output:
file("*") into roary_ch
script:
"""
panaroo -i *.gff -o ./ -t ${task.cpus} -a core --core_threshold 0.98 --clean-mode strict
"""
}
roary_ch.into{
roary_ch2
roary_ch3
roary_ch4
roary_ch5
roary_ch_filter
}
process iqtree_core {
publishDir = "$output/mbovpan_results/phylogeny"
conda "$workflow.projectDir/envs/iqtree.yaml"
cpus threads
errorStrategy 'ignore'
input:
file(input) from roary_ch.collect()
output:
file("*") into iqtreecore_ch
script:
"""
snp-sites -o mbovpan_align.snp_only.fasta core_gene_alignment.aln
iqtree -s mbovpan_align.snp_only.fasta -m MFP -nt ${task.cpus} -bb 1000 -pre mbovis_core
"""
}
process filter_pan {
publishDir = "./"
conda "bioconda::blast=2.9.0 pandas"
debug 'true'
input:
file("*") from roary_ch_filter.collect()
output:
file('mbovis_filtered_cogs.csv')
script:
"""
# first determine the genes that are present and use blast to find their true annotation
blastn -query pan_genome_reference.fa -max_target_seqs 1 -db $workflow.projectDir/ref/mbovis_reference -out mb.out -outfmt "6 delim=, qseqid sseqid length qstart qend sstart send qlen slen"
echo "qseqid,sseqid,length,qstart,qend,sstart,send,qlen,slen" > heading.txt
cat heading.txt mb.out >> mb.out.csv
# once mb.out is created, we can use python to 1) create length %, 2) filter by 75% or higher, and 3) reduce repetitive blast results
python $workflow.projectDir/scripts/blast_result_filter.py gene_presence_absence.csv
"""
}
}
process multiqc {
publishDir = "$output/mbovpan_results/statistics"
conda "$workflow.projectDir/envs/multiqc.yaml"
input:
file(pre) from fastqc_ch1.collect().ifEmpty([])
file(post) from fastqc_ch2.collect().ifEmpty([])
output:
file("mbovpan_report*")
script:
"""
multiqc -n mbovpan_report .
"""
}
process mbovis_verification {
publishDir = "$output/mbovpan_results/lineage_info"
input:
file(spoligotype_info) from spoligo_multi.collect().ifEmpty([])
file(lineage_info) from tbprofile_ch.collect().ifEmpty([])
output:
file("mbovpan_lineage_info.csv")
script:
"""
python ${lineage_table} > mbovpan_lineage_info.csv
"""
}