forked from darcyabjones/pclust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.nf
executable file
·273 lines (212 loc) · 5.97 KB
/
prepare.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
#!/usr/bin/env nextflow
/*
vim: syntax=groovy
-*- mode: groovy;-*-
*/
def helpMessage() {
log.info"""
=================================
pclust/prepare
=================================
Prepares predicted proteomes for processing through the pclust pipeline(s).
Because we use multiple proteomes in our studies, we need a way of making
individual protein ids unique within a proteome and an easy way of finding
which species a protein comes from.
This pipeline adds tags to the start of sequences to make this possible.
Usage:
```bash
nextflow run -resume prepare.nf \
--genomes "*.fasta" \
--gffs "*.gff3" \
--proteins "*.faa"
```
## Mandatory Arguments
```
param | description
--------------------------------------------------
`--genomes <fasta>` | The input genome fasta files.
`--gffs <gff3>` | The gff3 files containing genes to extract
| from genomes.
`--proteins <fasta>` | The protein fasta files to use directly.
```
Note that either the genomes and gffs must be provided or the proteins
must be provided. The pipeline can't run without input, or with only genomes
and proteins (no gff).
Outputs:
* `sequences/gff_id_map.tsv`:
The mapping of the original GFF3 ID protein names to the new ones.
* `sequences/proteins.faa`:
The combined processed fasta protein files.
""".stripIndent()
}
params.genomes = false
params.gffs = false
params.proteins = false
if (params.help){
helpMessage()
exit 0
}
/*
* Validate parameters.
*/
if ( params.gffs && !params.genomes ) {
log.info "Extracting GFFs requires fasta genomes."
exit 1
}
if ( !((params.gffs && params.genomes) || params.proteins) ) {
log.info "You must input either gffs+genomes or proteins."
exit 1
}
if ( params.genomes ) {
genomes = Channel
.fromPath( params.genomes )
.map { [it.baseName, it] }
}
if ( params.gffs && params.genomes ) {
gffs = Channel
.fromPath( params.gffs )
.map { [it.baseName, it] }
/*
* Duplicate the ID and Parent fields in the GFF into new attributes,
* so that we can keep track of them after gt tidies them.
*/
process duplicateIds {
label "R"
tag { label }
input:
set val(label), file("input.gff3") from gffs
output:
set val(label), file("${label}.gff3") into gffsDuplicatedIds
"""
duplicate_gff_id_attribute.R input.gff3 > "${label}.gff3"
"""
}
/*
* Tidy GFF3s so that genometools doesn't panic.
* Note that we rename input files to avoid name clashes.
*/
process tidyGFFs1 {
label "genometools"
tag { label }
input:
set val(label), file("input.gff3") from gffsDuplicatedIds
output:
set val(label), file("${label}.gff3") into tidiedGffs1
"""
gt gff3 \
-tidy \
-sort \
input.gff3 \
> "${label}.gff3"
"""
}
/*
* Add the filename to the IDs in the GFF, so that we know which
* genome a protein came from.
*/
process addFnameToId {
label "R"
tag { label }
input:
set val(label), file("${label}.gff3") from tidiedGffs1
output:
set val(label), file("out.gff3") into gffsWithFilenames
set val(label), file("out.tsv") into gffFilenameMaps
"""
add_filename_to_gff.R "${label}.gff3" out.gff3 out.tsv
"""
}
/*
* Combine all map tables into a single file.
*/
combinedGffMaps = gffFilenameMaps.collectFile(
name: "gff_id_map.tsv",
storeDir: "sequences",
sort: "hash",
newLine: true
)
/*
* Tidy GFF3s so that genometools doesn't panic.
* For whatever reason, a second tidy is needed to iron out the kinks for
* gt.
* Note that we rename input files to avoid name clashes.
*/
process tidyGFFs2 {
label "genometools"
tag { label }
input:
set val(label), file("input.gff3") from gffsWithFilenames
output:
set val(label), file("${label}.gff3") into tidiedGffs2
"""
gt gff3 \
-tidy \
-sort \
-retainids \
input.gff3 \
> "${label}.gff3"
"""
}
/*
* Join gff and genome channels together.
*/
genomesGffs = genomes.join(tidiedGffs2, by: 0)
/*
* Extract protein sequences from genomes and GFF.
*/
process extractProteins {
label "genometools"
tag { label }
input:
set val(label), file(fasta), file(gff) from genomesGffs
output:
set val(label), file("${label}.faa") into gffProteins
"""
gt extractfeat \
-type CDS \
-translate \
-matchdescstart \
-join \
-retainids \
-seqfile "${fasta}" \
"${gff}" \
> "${label}.faa"
"""
}
} else {
gffProteins = Channel.empty()
}
if ( params.proteins ) {
/*
* Using the protein option we just need to add filenames to sequences.
*/
raw_proteins = Channel
.fromPath( params.proteins )
.map { [it.baseName, it] }
/*
* Add filename to the sequence ids.
*/
process addFnameToProtId {
label "posix"
tag { label }
input:
set val(label), file("input.fasta") from raw_proteins
output:
set val(label), file("${label}.faa") into proteinProteins
"""
sed "s~>\\s*~>${label}_~g" < input.fasta > "${label}.faa"
"""
}
} else {
proteinProteins = Channel.empty()
}
proteins = gffProteins.concat( proteinProteins )
/*
* Combine all proteins into a single fasta file.
*/
combinedFasta = proteins.map {l, f -> f} .collectFile(
name: "proteins.faa",
storeDir: "sequences",
sort: "hash",
newLine: true
)