forked from epi2me-labs/wf-alignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nf
419 lines (380 loc) · 12.2 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
#!/usr/bin/env nextflow
import groovy.json.JsonBuilder
nextflow.enable.dsl = 2
include { fastq_ingress; xam_ingress; } from "./lib/ingress"
include { process_references } from "./subworkflows/process_references"
OPTIONAL_FILE = file("$projectDir/data/OPTIONAL_FILE")
MINIMAP_ARGS_PRESETS = [
"dna": "-ax map-ont -y",
"rna": "-ax splice -uf -y"
]
// Create an MMI index
process makeMMIndex {
label "wfalignment"
cpus params.threads
memory {
def ref_size = combined_refs.size()
combined_refs.size() > 1e9 ? "31 GB" : "11 GB"
}
input:
path combined_refs, stageAs: "combined_references.fasta"
val minimap_args
output:
path "combined_references.mmi"
script:
"""
minimap2 -t $task.cpus $minimap_args -d combined_references.mmi combined_references.fasta
"""
}
// Check if an MMI file contains the same references as the FASTA reference file.
process checkReferences {
label "wfalignment"
cpus params.threads
memory {
def ref_size = combined_refs.size()
combined_refs.size() > 1e9 ? "31 GB" : "11 GB"
}
input:
path "combined_references.mmi"
path "combined_refs.fasta.fai"
path combined_refs, stageAs: "combined_references.fasta"
output:
val true
script:
"""
# Read MMI references and check if they are in the FASTA fai file.
workflow-glue check_reference_index --mmi_file combined_references.mmi --fasta_fai combined_refs.fasta.fai
"""
}
process alignReads {
label "wfalignment"
cpus params.threads
memory {
combined_refs.size() > 1e9 ? "31 GB" : "11 GB"
}
input:
tuple val(meta), path(input)
path combined_refs
val is_xam
val minimap_args
output:
tuple val(meta), path(bam_name)
script:
def sample_name = meta["alias"]
bam_name = "${sample_name}.sorted.aligned.bam"
int sorting_threads = Math.min((task.cpus / 3) as int, 3)
int mapping_threads = task.cpus - sorting_threads
// the minimum for `params.threads` in the schema is `4` and we should have
// positive values for both thread vars, but can't hurt to make extra sure
sorting_threads = Math.max(1, sorting_threads)
mapping_threads = Math.max(1, mapping_threads)
"""
${is_xam ? "samtools fastq -T '*' $input" : "cat $input"} \
| minimap2 -t $mapping_threads $minimap_args $combined_refs - \
| samtools sort -@ ${sorting_threads - 1} -o $bam_name -
"""
}
process indexBam {
label "wfalignment"
cpus 1
memory "2 GB"
input:
tuple val(meta), path(bam)
output:
tuple val(meta), path(bam), path("*.bai")
script:
"""
samtools index $bam
"""
}
process bamstats {
label "wfalignment"
cpus 2
memory "4 GB"
input:
tuple val(meta), path(bam), path(index)
output:
path "*.readstats.tsv", emit: read_stats
path "*.flagstat.tsv", emit: flagstat
script:
def sample_name = meta["alias"]
"""
bamstats $bam -s $sample_name -u -f ${sample_name}.flagstat.tsv -t $task.cpus \
> ${sample_name}.readstats.tsv
"""
}
process addStepsColumn {
// TODO: we don't need 200 windows for very short references; find heuristics for
// determining window length / number for such cases
label "wfalignment"
cpus 1
memory "2 GB"
input: path "lengths.tsv"
output: path "lengths_with_steps.tsv"
"""
#!/usr/bin/env python
import pandas as pd
all = pd.read_csv('lengths.tsv', sep='\\t')
all["step"] = all["lengths"]//200
all = all.replace(0, 1)
all.to_csv('lengths_with_steps.tsv', index=False, header=False, sep='\\t')
"""
}
process readDepthPerRef {
// TODO: check if parallelisation with `xargs` or `parallel` is more efficient
label "wfalignment"
cpus 3
memory "7 GB"
input:
tuple val(meta), path(alignment), path(index)
path ref_len
output:
path outfname
script:
def sample_name = meta["alias"]
outfname = "${sample_name}.all_regions.bed.gz"
"""
while IFS=\$'\\t' read -r name lengths steps; do
mosdepth -n --fast-mode --by "\$steps" --chrom "\$name" -t $task.cpus \
${sample_name}."\$name".temp $alignment \
|| echo "No alignments for "\$name""
[[ -f ${sample_name}."\$name".temp.regions.bed.gz ]] && \
cat ${sample_name}."\$name".temp.regions.bed.gz >> $outfname
done < $ref_len
# remove all the temp files
find -name '${sample_name}.*.temp*' -delete
"""
}
process makeReport {
label "wfalignment"
cpus 1
memory "11 GB"
input:
path "readstats/*"
path "flagstat/*"
path "refnames/*"
path depths, stageAs: "depths/*"
path counts
path versions
path params
output:
path "*.html"
script:
String depth_args = "--depths_dir depths"
// we need to check against `.baseName` here because Nextflow includes the staging
// directory in the `.name` of a `TaskPath`
if (!(depths instanceof List) && depths.baseName == OPTIONAL_FILE.name) {
depth_args = ""
}
String counts_args = (counts.name == OPTIONAL_FILE.name) ? "" : "--counts $counts"
"""
workflow-glue report \
--name wf-alignment \
--stats_dir readstats \
--flagstat_dir flagstat \
--refnames_dir refnames \
--versions $versions \
--params $params \
$depth_args \
$counts_args
"""
}
process getVersions {
label "wfalignment"
cpus 1
memory "2 GB"
output:
path "versions.txt"
script:
"""
python --version | tr -s ' ' ',' | tr '[:upper:]' '[:lower:]' > versions.txt
seqkit version | sed 's/ /,/' >> versions.txt
minimap2 --version | sed 's/^/minimap2,/' >> versions.txt
samtools --version | (head -n 1 && exit 0) | sed 's/ /,/' >> versions.txt
fastcat --version | sed 's/^/fastcat,/' >> versions.txt
mosdepth --version | sed 's/ /,/' >> versions.txt
ezcharts --version | sed 's/ /,/' >> versions.txt
python -c "import pysam; print(f'pysam,{pysam.__version__}')" >> versions.txt
bgzip --version | head -n1 | sed -E 's/(.*) /\\1,/' >> versions.txt
"""
}
process getParams {
label "wfalignment"
cpus 1
memory "2 GB"
output:
path "params.json"
script:
def paramsJSON = new JsonBuilder(params).toPrettyString()
"""
# Output nextflow params object to JSON
echo '$paramsJSON' > params.json
"""
}
// workflow module
workflow pipeline {
take:
sample_data
refs
counts
depth_coverage
main:
// get params & versions
workflow_params = getParams()
software_versions = getVersions()
// minimap2 args
String minimap_args
minimap_args = params.minimap_args ?: \
MINIMAP_ARGS_PRESETS[params.minimap_preset]
// handle references
// if params.references contains MMI index file
// use this as reference
combined_mmi_file = Channel.of(OPTIONAL_FILE)
// Process references although input is an MMI index
// as Jbrowse needs the processed FASTA file
refs = process_references(params.references)
if (params.reference_mmi_file) {
log.info("Using the provided MMI index as reference.")
log.info("Indexing parameters (-k, -w or -H) will be overridden by parameters used in the prebuilt index.")
minimap_reference = Channel.fromPath(params.reference_mmi_file, checkIfExists: true).first()
// make sure mmi index contains the same references as the fasta
checkReferences(minimap_reference, refs.combined_index, refs.combined)
} else {
minimap_reference = makeMMIndex(refs.combined, minimap_args)
}
sample_data = sample_data
| map { meta, path, stats -> [meta, path] }
if (params.bam) {
ch_branched = sample_data.branch { meta, bam ->
to_align: meta["is_unaligned"]
aligned: true
}
ch_to_align = ch_branched.to_align
// `xam_ingress` sorts the BAMs, so we don't have to
bam = ch_branched.aligned
} else {
// FASTQ input
ch_to_align = sample_data
bam = Channel.empty()
}
// run minimap
bam = bam
| mix(
alignReads(ch_to_align, minimap_reference, params.bam as boolean, minimap_args)
)
| indexBam
// get stats
stats = bamstats(bam)
// determine read_depth per reference / bam file if requested
depth_per_ref = Channel.of(OPTIONAL_FILE)
if (depth_coverage) {
// add step column to ref lengths
ref_lengths_with_steps = addStepsColumn(refs.lengths_combined)
depth_per_ref = readDepthPerRef(bam, ref_lengths_with_steps)
}
report = makeReport(
stats.read_stats.collect(),
stats.flagstat.collect(),
refs.names_per_ref_file.collect(),
depth_per_ref.collect(),
counts,
software_versions,
workflow_params,
)
emit:
alignments = bam.map { it[1] }
indices = bam.map{ it[2] }
per_read_stats = stats.read_stats
per_file_stats = stats.flagstat
report
params_json = workflow_params
software_versions
combined_ref = refs.combined
combined_ref_index = refs.combined_index
combined_ref_mmi_file = minimap_reference
}
// 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 {
label "wfalignment"
cpus 1
memory "2 GB"
// publish inputs to output directory
publishDir "${params.out_dir}", mode: 'copy', pattern: "*", saveAs: {
f -> params.prefix ? "${params.prefix}-${f}" : "${f}" }
input:
path fname
output:
path fname
"""
echo "Writing output files"
"""
}
process configure_jbrowse {
label "wfalignment"
cpus 1
memory { reference.size() > 1e9 ? "15 GB" : "2 GB" }
input:
path(alignments)
path(indexes)
path(reference)
path(ref_idx)
output:
path("jbrowse.json")
script:
ArrayList alignment_args = []
int i = 0;
for(a in alignments) {
// don't be fooled into iterating over bam.size() here
// when the cardinality is 1, bam.size() returns the filesize of the bam!
this_bam = a
this_bai = indexes[i]
alignment_args << "--alignment '${params.out_dir}/${this_bam.name}' '${params.out_dir}/${this_bai.name}'"
i++;
}
String alignment_args_str = alignment_args.join(' ')
"""
workflow-glue configure_jbrowse \
--reference '${reference}' '${params.out_dir}/${reference.name}' '${params.out_dir}/${ref_idx.name}' \
${alignment_args_str} > jbrowse.json
"""
}
// entrypoint workflow
WorkflowMain.initialise(workflow, params, log)
workflow {
Pinguscript.ping_start(nextflow, workflow, params)
Map ingress_args = [
"sample": params.sample,
"sample_sheet": params.sample_sheet,
"analyse_unclassified": params.analyse_unclassified,
"stats": false,
]
// get input data
if (params.fastq) {
sample_data = fastq_ingress(ingress_args + ["input": params.fastq])
} else {
sample_data = xam_ingress(
ingress_args + ["input": params.bam, "keep_unaligned": true]
)
}
counts = file(params.counts ?: OPTIONAL_FILE, checkIfExists: true)
// Run pipeline
results = pipeline(
sample_data, params.references, counts, params.depth_coverage
)
// create jbrowse file
jb2_conf = configure_jbrowse(
results.alignments.collect(),
results.indices.collect(),
results.combined_ref,
results.combined_ref_index
)
output(jb2_conf.concat(results))
}
workflow.onComplete {
Pinguscript.ping_complete(nextflow, workflow, params)
}
workflow.onError {
Pinguscript.ping_error(nextflow, workflow, params)
}