forked from epi2me-labs/wf-bacterial-genomes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.nf
257 lines (216 loc) · 6.05 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
#!/usr/bin/env extflow
nextflow.enable.dsl = 2
def helpMessage(){
log.info """
Haploid Variant Analysis Workflow
Usage:
nextflow run epi2me-labs/wf-hap-snp [options]
Options:
--fastq DIR Path to FASTQ directory (required)
--reference FILE Reference sequence FASTA file (required)
--out_dir DIR Path for output (default: $params.out_dir)
--medaka_model STR Medaka model name (default: $params.medaka_model)
--run_prokka BOOL Run prokka on consensus sequence (default: $params.run_prokka)
--prokka_opts STR Command-line arguments for prokka (default: $params.prokka_opts)
"""
}
process concatFastq {
label "containerCPU"
cpus 1
input:
file "input"
output:
file "reads.fastq"
shell:
"""
# TODO: could do better here
fastcat -r summary.txt input/*.fastq* > reads.fastq
"""
}
process readStats {
label "containerCPU"
cpus 1
input:
tuple file("alignments.bam"), file("alignments.bam.bai")
output:
path "readstats.txt", emit: stats
path "mapsummary.txt", emit: summary
"""
stats_from_bam -s mapsummary.txt -o readstats.txt alignments.bam
if [[ \$(wc -l <readstats.txt) -le 1 ]]; then
echo "No alignments of reads to reference sequence found."
exit 1
fi
"""
}
process coverStats {
label "containerCPU"
cpus 2
input:
tuple file("alignments.bam"), file("alignments.bam.bai")
output:
file "depth.txt"
"""
coverage_from_bam --one_file depth.txt --stride 100 alignments.bam
"""
}
process alignReads {
label "containerCPU"
cpus params.threads
input:
file reads
file reference
output:
tuple file("reads2ref.bam"), file("reads2ref.bam.bai")
"""
mini_align -i $reads -r $reference -p reads2ref -t $task.cpus -m
"""
}
process splitRegions {
// split the bam reference sequences into overlapping sub-regions
label "containerCPU"
cpus 1
input:
tuple file(bam), file(bai)
output:
stdout
"""
#!/usr/bin/env python
import itertools
import medaka.common
regions = itertools.chain.from_iterable(
x.split($params.chunk_size, overlap=1000, fixed_size=False)
for x in medaka.common.get_bam_regions("$bam"))
for reg in regions:
print(reg)
"""
}
// TODO: in a single GPU environment it would be better just
// to use a single process for the whole bam file. Need
// to read up on conditional channels
process medakaNetwork {
// run medaka consensus for each region
label "containerCPU"
cpus 2
input:
tuple file(bam), file(bai)
each reg
output:
file "consensus_probs.hdf"
"""
medaka consensus $bam consensus_probs.hdf --threads 2 --model $params.medaka_model --region "$reg"
"""
}
process medakaVariant {
label "containerCPU"
cpus 1
input:
file "consensus_probs_*.hdf"
tuple file(bam), file(bai)
file reference
output:
tuple path("medaka.vcf.gz"), path("medaka.vcf.gz.gzi")
"""
medaka variant $reference consensus_probs_*.hdf vanilla.vcf
medaka tools annotate vanilla.vcf $reference $bam medaka.vcf
bgzip -i medaka.vcf
"""
}
process medakaConsensus {
label "containerCPU"
cpus 1
input:
file "consensus_probs_*.hdf"
file reference
output:
file "medaka.fasta.gz"
"""
medaka stitch --threads $task.cpus consensus_probs_*.hdf $reference medaka.fasta
bgzip medaka.fasta
"""
}
process runProkka {
// run prokka in a basic way on the consensus sequence
label "prokka"
cpus 1
input:
file "consensus.fasta"
output:
file "prokka_results"
"""
prokka ${params.prokka_opts} --outdir prokka_results --prefix prokka consensus.fasta
"""
}
process makeReport {
label "containerCPU"
cpus 1
input:
file "depth.txt"
file "read_summary.txt"
file "align_summary.txt"
tuple path("medaka.vcf.gz"), path("medaka.vcf.gz.gzi")
output:
file "wf-hap-snps-report.html"
"""
bcftools stats medaka.vcf.gz > variants.stats
report.py depth.txt read_summary.txt align_summary.txt variants.stats wf-hap-snps-report.html
"""
}
// See https://github.com/nextflow-io/nextflow/issues/1636
// This is the only way to publish files from a workflow whilst
// decoupling the publish from the process steps.
process output {
// publish inputs to output directory
label "containerCPU"
publishDir "${params.out_dir}", mode: 'copy', pattern: "*"
input:
file fname
output:
file fname
"""
echo "Writing output files"
"""
}
// modular workflow
workflow calling_pipeline {
take:
reads
reference
main:
reads = concatFastq(reads)
alignments = alignReads(reads, reference)
read_stats = readStats(alignments)
depth_stats = coverStats(alignments)
regions = splitRegions(alignments).splitText()
hdfs = medakaNetwork(alignments, regions).collect()
consensus = medakaConsensus(hdfs, reference)
variants = medakaVariant(hdfs, alignments, reference)
if (params.run_prokka) {
prokka = runProkka(consensus)
} else {
prokka = Channel.empty()
}
report = makeReport(depth_stats, read_stats.stats, read_stats.summary, variants)
emit:
consensus
variants
report
prokka
}
// entrypoint workflow
workflow {
if (params.help) {
helpMessage()
exit 1
}
if (!params.fastq or !params.reference) {
helpMessage()
println("")
println("`--fastq` and `--reference` are required")
exit 1
}
reads = channel.fromPath(params.fastq, type:'dir', checkIfExists:true)
reference = channel.fromPath(params.reference)
results = calling_pipeline(reads, reference)
output(results.consensus.concat(results.variants, results.report, results.prokka))
}