-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.nf
executable file
·492 lines (366 loc) · 10.7 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
#!/usr/bin/env nextflow
/*
vim: syntax=groovy
-*- mode: groovy;-*-
*/
params.genomes = false
params.references = false
params.genes = false
params.promer = false
if ( params.references ) {
references = Channel.fromPath(
params.references,
checkIfExists: true,
type: "file"
)
} else {
log.info "Hey I need some reference genomes please!"
exit 1
}
if ( params.genomes ) {
genomes = Channel.fromPath(
params.genomes,
checkIfExists: true,
type: "file"
)
} else {
log.info "Hey I need some genomes to align please!"
exit 1
}
if ( params.genes ) {
genes = Channel.fromPath(
params.genes,
checkIfExists: true,
type: "file"
).map { f -> [f.baseName, f ] }
} else {
genes = Channel.empty()
}
references.into {
references4Cross;
references4Snp2Vcf;
references4GenomeIndex;
references4MergeGenes;
}
// Exit if gff provided that can't be matched.
// Filter out refs without gff provided.
// Note we could use join instead of combine, which would allow us
// to get rid of the filter, but we wouldn't be able fail on unmatched gff.
refWithGenes = references4MergeGenes
.map { f -> [ f.baseName, f ] }
.combine( genes, by: 0 )
.map { n, f, g ->
if ( f == null || f == '' ) {
log.error "The annotation file ${g.name} specified in --genes could " +
"not be matched to any reference genome names."
log.error "Please make sure the annotation file and reference genomes " +
"have the same basename (up to the last extension)."
exit 1
};
[n, f, g]
}
.filter { n, f, g -> (g == null || g == '') }
pairs = references4Cross
.combine(genomes)
.filter { r, g -> r.name != g.name }
process mumalign {
label "mummer"
label "medium_task"
publishDir "${params.outdir}/alignments/${ref.baseName}"
tag { "${ref.baseName} - ${query.baseName}" }
input:
set file(ref), file(query) from pairs
output:
set val(ref.baseName), file("${query.baseName}.delta") into deltas
script:
if (params.promer) {
exe = "promer"
} else {
exe = "nucmer"
}
"""
${exe} \
--threads=${task.cpus} \
--maxmatch \
--prefix=${query.baseName} \
${ref} \
${query}
"""
}
process dnadiff {
label "mummer"
label "small_task"
publishDir "${params.outdir}/diffs/${ref}"
tag { "${ref} - ${delta.baseName}" }
input:
set val(ref), file(delta) from deltas
output:
set val(ref), file("${delta.baseName}.snps") into snps
set val(ref), file("${delta.baseName}.1coords") into onecoords
set val(ref), file("${delta.baseName}.1delta") into onedeltas
set val(ref), file("${delta.baseName}.mcoords") into mcoords
set val(ref), file("${delta.baseName}.mdelta") into mdeltas
set val(ref), file("${delta.baseName}.qdiff") into qdiff
set val(ref), file("${delta.baseName}.rdiff") into rdiff
set val(ref), file("${delta.baseName}.report") into dnadiffReports
set val(ref), file("${delta.baseName}.unqry") optional true into unqrys
"""
dnadiff --prefix=${delta.baseName} --delta ${delta}
"""
}
process snp2vcf {
label "python3"
label "small_task"
publishDir { "${params.outdir}/vcfs/${ref.baseName}" }
tag { "${ref.baseName} - ${snp.baseName}" }
input:
set file(ref), file(snp) from references4Snp2Vcf
.map { [it.baseName, it] }
.combine( snps, by: 0 )
.map { rn, r, s -> [r, s] }
output:
set val(ref.baseName), file("${snp.baseName}.vcf") into vcfs
"""
snps2vcf.py \
--snp ${snp} \
--reference ${ref} \
--name ${snp.baseName} \
--output ${snp.baseName}.vcf
"""
}
/*
* Convert coords to awk.
* Note that the output of show-coords and dnadiff are slightly different.
* This is for mcoords from dnadiff.
*/
process coordsToBED {
label "posix"
label "small_task"
publishDir "${params.outdir}/diffs/${ref}"
tag { "${ref} - ${coord.baseName}" }
input:
set val(ref), file(coord) from mcoords
output:
set val(ref), file("${coord.baseName}.bed") into coordsBEDs
"""
awk '{ printf "%s\\t%s\\t%s\\t\\.\\t+\\t%s\\tfiltered\\n", \$12, \$1-1, \$2, \$7 }' ${coord} \
| sort -k1,1 -k2,2n \
> ${coord.baseName}.bed
"""
}
process genomeIndex {
label "samtools"
label "small_task"
tag "${ref}"
input:
file ref from references4GenomeIndex
output:
set file(ref), file("${ref}.fai") into referenceIndex
"""
samtools faidx ${ref} > ${ref}.fai
"""
}
referenceIndex.into {
referenceIndex4BedCoverage;
referenceIndex4MakeWindows;
referenceIndex4MakePBWindows;
referenceIndex4PlotCoverages;
}
process bedCoverage {
label "bedtools"
label "small_task"
tag "${ref.baseName} - ${bed.baseName}"
input:
set file(ref), file(index), file(bed) from referenceIndex4BedCoverage
.map { r, i -> [r.baseName, r, i] }
.combine( coordsBEDs, by: 0 )
.map { rn, r, i, b -> [r, i, b] }
output:
set val(ref.baseName), file("${bed.baseName}.bedgraph") into coverageBEDs
"""
bedtools genomecov -bga -g ${index} -i ${bed} > ${bed.baseName}.bedgraph
"""
}
process combinedBEDCoverage {
label "bedtools"
label "small_task"
publishDir "${params.outdir}/bedgraphs/${ref}"
tag "${ref}"
input:
set val(ref), file("*") from coverageBEDs.groupTuple(by: 0)
output:
set val(ref), file("${ref}.bedgraph") into combinedCoverage
"""
NAMES=( *.bedgraph )
bedtools unionbedg \
-header \
-names \${NAMES[@]%.bedgraph} \
-i *.bedgraph \
> ${ref}.bedgraph
"""
}
combinedCoverage.into {
combinedCoverage4GetPBCoverage;
combinedCoverage4FindPAV;
}
window_sizes = [10000, 50000, 100000]
process makeWindows {
label "bedtools"
label "small_task"
tag "${window_size}"
input:
set file(ref), file(index) from referenceIndex4MakeWindows
each window_size from window_sizes
output:
set val(ref.baseName), val(window_size),
file("windows_${window_size}.bed") into windows
"""
bedtools makewindows -g "${index}" -w "${window_size}" > windows.tmp.bed
# Doing this in 2 steps (rather than piping) is necessary to avoid
# overlayfs requirements in singularity.
sort -k1,1 -k2,2n windows.tmp.bed > "windows_${window_size}.bed"
rm -f windows.tmp.bed
"""
}
process makePBWindows {
label "bedtools"
label "small_task"
tag "${ref.baseName}"
input:
set file(ref), file(index) from referenceIndex4MakePBWindows
output:
set val(ref.baseName), file("windows.bed") into pbWindows
"""
bedtools makewindows -g "${index}" -w 1 > windows.tmp.bed
# Doing this in 2 steps is necessary to avoid overlayfs
# requirements in singularity. (because sort uses tmp files).
sort -k1,1 -k2,2n windows.tmp.bed > windows.bed
rm -f windows.tmp.bed
"""
}
process getPBCoverage {
label "bedtools"
label "small_task"
tag "${ref}"
input:
set val(ref), file(pb_window), file(coverage) from pbWindows
.combine( combinedCoverage4GetPBCoverage, by: 0 )
output:
set val(ref), file("pbcov.bedgraph") into pbCoverages
"""
bedtools intersect \
-a "${coverage}" \
-b "${pb_window}" \
-sorted \
-header \
> "pbcov.bedgraph"
"""
}
process getMeanWindowedCoverage {
label "bedtools"
label "small_task"
tag "${ref} - ${window_size}"
publishDir "${params.outdir}/bedgraphs/${ref}"
input:
set val(ref), val(window_size), file("windows.bed"),
file("pbcov.bedgraph") from windows
.combine( pbCoverages, by: 0 )
output:
set val(ref), val(window_size),
file("cov_mean_${window_size}.bedgraph") into meanWindowedCoverage
"""
NCOLS=\$(head -n 1 pbcov.bedgraph | wc -w)
COLS=\$(seq 4 \${NCOLS} | tr '\n' ',' | sed 's/,\$//')
head -n 1 "pbcov.bedgraph" > "cov_mean_${window_size}.bedgraph"
bedtools map \
-a "windows.bed" \
-b "pbcov.bedgraph" \
-c "\${COLS}" \
-o mean \
>> "cov_mean_${window_size}.bedgraph"
"""
}
process plotCoverages {
label "r"
label "small_task"
tag "${ref} - ${window_size}"
publishDir "${params.outdir}/coverage_plots/${ref}"
input:
set val(ref), file(index), val(window_size), file(bg) from referenceIndex4PlotCoverages
.map { f, i -> [f.baseName, i] }
.combine( meanWindowedCoverage, by: 0 )
output:
set val(ref), val(window_size), file("${window_size}") into coveragePlots
"""
plot_circos.R --bedgraph "${bg}" --faidx "${index}" --outdir "${window_size}"
"""
}
process findPAV {
label "python3"
label "small_task"
tag "${ref}"
publishDir "${params.outdir}/pav/${ref}"
input:
set val(ref), file(bg) from combinedCoverage4FindPAV
output:
set val(ref), file("pavs.bedgraph") into foundPAVs
"""
find_pavs.py \
--infile "${bg}" \
--outfile pavs.bedgraph \
--tol 20 \
--min-length 50 \
--proportion-repeats 1.0
"""
}
process pavGenes {
label "bedtools"
label "small_task"
tag "${ref}"
publishDir "${params.outdir}/pav/${ref}"
input:
set val(ref), file("pavs.bedgraph"), file("genome.fasta"),
file(genes) from foundPAVs
.join( refWithGenes, remainder: false, by: 0 )
output:
set val(ref), file("gene_pavs.bedgraph") into genePAVs
"""
bedtools intersect \
-a pavs.bedgraph \
-b "${genes}" \
-header \
-u \
> gene_pavs.bedgraph
"""
}
/*
percentages = [1, 2, 3, 4, 5, 7, 10, 20, 30, 40, 50,
60, 70, 80, 90, 93, 95, 96, 97, 98, 99]
process compareCoverage {
label "python3"
label "small_task"
publishDir "${params.outdir}/core_accessory/${bg.baseName}/${perc}"
tag "${bg.baseName} - ${perc}"
input:
file bg from combinedCoverage
each val perc from percentages
output:
file "core.bedgraph" into coreRegions
file "duplicate.bedgraph" into duplicateRegions
file "single.bedgraph" into singleRegions
"""
/usr/bin/env python3
import os
import pandas as pd
table = pd.read_csv("${bg}", sep="\t")
table.columns = [os.path.split(os.path.splitext(c)[0])[1] for c in table.columns]
pc = ${perc} / 100
table2 = table[(table.iloc[:, 3:] > 0).mean(axis=1) > pc]
table2.to_csv("core.bedgraph", index=False, sep="\t")
table3 = table2[(table2.iloc[:, 3:] > 1).mean(axis=1) > pc]
table3.to_csv("duplicate.bedgraph", index=False, sep="\t")
table4 = table2[(table2.iloc[:, 3:] == 1).mean(axis=1) > pc]
table4.to_csv("single.bedgraph", index=False, sep="\t")
"""
}
*/