-
Notifications
You must be signed in to change notification settings - Fork 1
/
polish.nf
191 lines (145 loc) · 4.39 KB
/
polish.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
#!/usr/bin/env nextflow
/*
vim: syntax=groovy
-*- mode: groovy;-*-
*/
def helpMessage() {
log.info"""
# mitoflow - postprocessing
Filter out mitochondrial contigs from an assembly.
## Usage
```bash
nextflow run -resume ./post.nf "*{_genomic,_mitochondrial}.fasta"
```
Note that because of the way the glob works, you need something unique
in the bracket expansion for the pattern to match properly.
E.G. Say you had the files `genome.fasta` and `genome_mitochondrial.fasta`
the pattern, `*{,_mitochondrial}.fasta` doesn't work because
`genome_mitochondrial.fasta` will match twice.
It's annoying, but you might just have to rename your files. Sorry!
Requirements:
minimap2
Python3 with Biopython installed
""".stripIndent()
}
if (params.help){
helpMessage()
exit 0
}
params.reference = false
params.asm_table = false
if ( params.asm_table ) {
asmTable = Channel.fromPath(params.asm_table)
.splitCsv(by: 1, sep: '\t', header: true)
.map { [it.assembly, file(it.read1_file), file(it.read2_file)] }
} else {
log.info "Hey I need some reads to assemble into a mitochondrial genome."
exit 1
}
if ( params.reference ) {
reference = Channel.fromPath(params.reference, checkIfExists: true, type: "file")
.collectFile(name: 'reference.fasta', newLine: true, sort: "deep")
.first()
} else {
log.info "Hey I need a mitochondrial reference."
exit 1
}
asmTable.into {
asmTable4Recycle;
asmTable4ReadAlignment;
}
process recycle {
label "hced"
tag { assembly.simpleName }
publishDir "${params.outdir}/recycled"
input:
file assembly from asmTable4Recycle
.map { a, r1, r2 -> a }
.unique()
file reference from reference
output:
set val(assembly.name), file("${assembly.baseName}_recycled.fasta") into recycledFasta
"""
# The reference (one that is not cycled) should be second sequence in file.
cat ${assembly} > combined.fasta
# In case no trailing newline.
echo "" >> combined.fasta
cat ${reference} >> combined.fasta
hCED \
--input-file combined.fasta \
--output-file recycled.fasta
# Takes just the first seq from multifasta
awk 'BEGIN { NSEQS=0 } /^>/ {NSEQS++} NSEQS < 2 {print $0} ' \
recycled.fasta > ${assembly.baseName}_recycled.fasta
"""
}
recycledFasta.into {
recycledFasta4MSA;
recycledFasta4IndexAssembly;
}
process msa {
label "mafft"
publishDir "${params.outdir}/msa"
input:
file "*.fasta" from recycledFasta4MSA
.map { n, f -> f}
.collect()
file "reference.fa" from reference
output:
file "aligned.fasta" into alignedAssemblies
"""
cat reference.fa > combined.fasta
cat *.fasta >> combined.fasta
mafft combined.fasta > aligned.fasta
"""
}
process indexAssembly {
label "bwa"
tag { asm_name }
input:
set val(asm_name), file(asm) from recycledFasta4IndexAssembly
output:
set val(asm_name), file("index") into indexedAssembly
"""
mkdir index
cp ${asm} index/ref.fa
samtools faidx index/ref.fa
bwa index -p index/ref ${asm}
"""
}
joined4ReadAlignment = indexedAssembly
.combine(asmTable4ReadAlignment.map { n, r1, r2 -> [n.name, r1, r2]}, by: 0)
process readAlignment {
label "bwa"
tag { asm_name }
input:
set val(asm_name), file("index"), file(read1_file),
file(read2_file) from joined4ReadAlignment
output:
set val(asm_name), file("aligned.sam") into alignedReads
"""
bwa mem index/ref ${read1_file} ${read2_file} > aligned.sam
"""
}
process combineAlignments {
label "samtools"
tag { asm_name }
publishDir "read_alignment"
input:
set val(asm_name), file("*.sam") from alignedReads
.groupTuple(by: 0)
output:
set val(asm_name), file("${asm_name}.bam"),
file("${asm_name}.bam.bai") into combinedAlignedReads
set val(asm_name), file("${asm_name}.idxstats"),
file("${asm_name}.flagstats"), file("${asm_name}.stats") into alignedStats
"""
cat *.sam > combined.sam
samtools sort -O bam -o "${asm_name}.bam" combined.sam
samtools index "${asm_name}.bam"
samtools idxstats "${asm_name}.bam" > "${asm_name}.idxstats"
samtools flagstat "${asm_name}.bam" > "${asm_name}.flagstat"
samtools stats "${asm_name}.bam" > "${asm_name}.stats"
rm combined.sam
"""
}