-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.nf
319 lines (282 loc) · 9.12 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
#!/usr/bin/env nextflow
// Developer notes
//
// This template workflow provides a basic structure to copy in order
// to create a new workflow. Current recommended practices are:
// i) create a simple command-line interface.
// ii) include an abstract workflow scope named "pipeline" to be used
// in a module fashion
// iii) a second concreate, but anonymous, workflow scope to be used
// as an entry point when using this workflow in isolation.
import groovy.json.JsonBuilder
nextflow.enable.dsl = 2
include { fastq_ingress; xam_ingress } from './lib/ingress'
OPTIONAL_FILE = file("$projectDir/data/OPTIONAL_FILE")
process getVersions {
label "wf_common"
cpus 1
output:
path "versions.txt"
script:
"""
python --version | sed 's/^/python,/' >> versions.txt
fastcat --version | sed 's/^/fastcat,/' >> versions.txt
python -c "import pysam; print(f'pysam,{pysam.__version__}')" >> versions.txt
python -c "import numpy; print(f'numpy,{numpy.__version__}')" >> versions.txt
python -c "import pandas; print(f'pandas,{pandas.__version__}')" >> versions.txt
"""
}
process addEmuToVersions {
label "wf_emu"
cpus 1
input:
path "common_versions.txt"
output:
path "versions.txt"
script:
"""
emu --version | sed 's/^/emu,/' >> common_versions.txt
minimap2 --version | sed 's/^/minimap2,/' >> common_versions.txt
mv common_versions.txt versions.txt
"""
}
process getParams {
label "wf_common"
cpus 1
output:
path "params.json"
script:
def paramsJSON = new JsonBuilder(params).toPrettyString()
"""
# Output nextflow params object to JSON
echo '$paramsJSON' > params.json
"""
}
process unpackDatabase {
label "wf_emu"
cpus 1
storeDir "${params.store_dir}/${params.database_set}"
input:
val database
output:
path "database_dir/${params.database_set}"
script:
def db_name = "${database}".tokenize('/').last()
"""
mkdir database_dir
osf -p 56uf7 fetch "${database}"
tar -xf "${db_name}" -C database_dir/
"""
}
process mapReads {
label "wf_emu"
cpus params.threads
input:
tuple val(meta), path(concat_seqs), path(stats)
path database
output:
tuple(
val(meta), path("*.sam"), path(stats), emit: sam)
script:
def sample_id = meta["alias"]
"""
minimap2 -ax map-ont "${database}/species_taxid.fasta" "${concat_seqs}" -t "${task.cpus}" -N "${params.num_alignments}" -p 0.9 -K "${params.K}" -o "${sample_id}.sam"
"""
}
//Emu uses SAM files
process bam2sam {
label "wf_common"
cpus params.threads
input:
tuple val(meta), path(concat_seqs), path(stats)
output:
tuple(
val(meta), path("*.sam"), path(stats), emit: sam)
script:
def sample_id = meta["alias"]
"""
samtools view -h "${concat_seqs}" > "${sample_id}.sam"
"""
}
process runEmu {
label "wf_emu"
cpus params.threads
input:
tuple val(meta), path(sam), path(stats)
path database
output:
tuple(
val(meta),
path("emu_results/*_read-assignment-distributions.tsv"),
path("emu_results/*_rel-abundance.tsv"),
path("emu_results/*_unclassified.fa"),
emit:emu_results)
script:
def sample_id = meta["alias"]
def output_unclassified = "${params.output_unclassified}"!= null ? "--output-unclassified" : ""
def keep_read_assignments = "${params.keep_read_assignments}"!= null ? "--keep-read-assignments" : ""
def keep_counts = "${params.keep_counts}"!= null ? "--keep-counts" : ""
"""
emu abundance ${sam} --output-dir emu_results --db ${database} --min-abundance ${params.min_abundance} --output-basename ${sample_id} \
${keep_counts} ${keep_read_assignments} ${output_unclassified}
"""
}
process combineOutput {
label "wf_emu"
cpus params.threads
input:
path emu_results
output:
path "*.tsv" , emit: combine_output
script:
def split_tables = params.split_tables ? "--split-tables" : ""
def counts = params.counts ? "--counts" : ""
"""
emu combine-outputs . ${params.rank} ${counts} ${split_tables}
"""
}
process makeReport {
label "wf_metagenomics"
input:
val metadata
path "read_stats/per-read-stats*.tsv.gz"
path rel_abundance_tsv
path "versions/*"
path "params.json"
output:
path "wf-emu-*.html", emit: report_html
script:
String report_name = "wf-emu-report.html"
String metadata = new JsonBuilder(metadata).toPrettyString()
def stats_args = params.wf.stats ? "--read_stats read_stats/*" : ""
"""
echo '${metadata}' > metadata.json
workflow-glue report $report_name \
--versions versions \
${stats_args} \
--rel_abun $rel_abundance_tsv\
--params params.json \
--metadata metadata.json
"""
}
// 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 "wf_common"
publishDir (
params.out_dir,
mode: "copy",
saveAs: { dirname ? "$dirname/$fname" : fname }
)
input:
tuple path(fname), val(dirname)
output:
path fname
"""
echo "Writing output files"
"""
}
// workflow module
workflow pipeline {
Pinguscript.ping_start(nextflow, workflow, params)
take:
samples
input_type
database
main:
common_versions = getVersions()
software_versions = addEmuToVersions(common_versions)
workflow_params = getParams()
database = unpackDatabase(database)
// Initial reads QC
// bamstats files have a different name
per_read_stats = samples.map {it[2].resolve('*.tsv.gz')}.collect()
metadata = samples.map { it[0] }.toList()
samples.view()
// Run Minimap2
if (input_type == 'fastq') {
minimap_alignments = mapReads(
samples
| map { [it[0], it[1], it[2] ?: OPTIONAL_FILE ] },
database
)
} else{
minimap_alignments = bam2sam(
samples
| map { [it[0], it[1], it[2] ?: OPTIONAL_FILE ] },
)
}
emu = runEmu(
minimap_alignments
| map{[it[0], it[1], it[2]]},
database
)
emu_rel_abun = emu.emu_results.map { it[2] }.collect()
combine_output = combineOutput(emu_rel_abun)
report = makeReport(
metadata,
per_read_stats,
emu_rel_abun,
software_versions,
workflow_params
)
ch_to_publish = Channel.empty()
| mix(
software_versions,
workflow_params,
report.report_html,
combine_output.combine_output | flatten,
)
| map { [it, null] }
ch_to_publish = ch_to_publish | mix (
emu.emu_results | map { meta, read_assignment, rel_abundance, unclassified -> [rel_abundance, "rel_abun"]},
emu.emu_results | map { meta, read_assignment, rel_abundance, unclassified -> [unclassified, "unclassified"]},
emu.emu_results | map { meta, read_assignment, rel_abundance, unclassified -> [read_assignment, "read_assignment"]},
)
ch_to_publish | output
emit:
report
}
// entrypoint workflow
WorkflowMain.initialise(workflow, params, log)
workflow {
ArrayList fastcat_extra_args = []
if (params.min_len) { fastcat_extra_args << "-a $params.min_len" }
if (params.max_len) { fastcat_extra_args << "-b $params.max_len" }
if (params.min_read_qual) { fastcat_extra_args << "-q $params.min_read_qual" }
// Check source param is valid
sources = params.database_sets
source_name = params.database_set
source_data = sources.get(source_name, false)
if (!sources.containsKey(source_name) || !source_data) {
keys = sources.keySet()
throw new Exception("Source $params.source is invalid, must be one of $keys")
}
// Grab database files
database = sources[source_name]["database"]
def input_type = ['fastq', 'bam'].findAll { params[it] }
if (input_type.size() != 1) {
error "Only provide one of '--fastq', '--bam'."
}
input_type = input_type[0]
if (input_type == 'fastq') {
samples = fastq_ingress([
"input":params.fastq,
"sample":params.sample,
"sample_sheet":params.sample_sheet,
"analyse_unclassified":params.analyse_unclassified,
"stats": params.wf.stats,
"required_sample_types": [],
"fastcat_extra_args": fastcat_extra_args.join(" "),
"watch_path": false])
}
results = pipeline(samples, input_type, database)
}
workflow.onComplete {
Pinguscript.ping_complete(nextflow, workflow, params)
}
workflow.onError {
Pinguscript.ping_error(nextflow, workflow, params)
}