-
Notifications
You must be signed in to change notification settings - Fork 4
/
annotation.nf
327 lines (250 loc) · 8.11 KB
/
annotation.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
#!/usr/bin/env nextflow
// Processes to perform de novo assembly and annotate those assembled sequences
nextflow.enable.dsl=2
container__eggnogmapper = 'quay.io/biocontainers/eggnog-mapper:2.1.12--pyhdfd78af_0'
container__diamond = 'quay.io/biocontainers/diamond:2.1.8--h43eeafb_0'
// Using DSL-2
nextflow.enable.dsl=2
// Parameters
params.gene_fasta = false
params.output = './results/'
params.help = false
params.min_coverage = 50
params.min_identity = 90
params.taxonomic_dmnd = false
params.ncbi_taxdump = "ftp://ftp.ncbi.nlm.nih.gov/pub/taxonomy/taxdump.tar.gz"
params.eggnog_db = false
params.eggnog_dmnd = false
params.noannot = false
// Make sure that --output ends with trailing "/" characters
if (!params.output.endsWith("/")){
params.output_folder = params.output.concat("/")
} else {
params.output_folder = params.output
}
process shard_genes {
tag "Split the gene catalog into smaller shards"
container "ubuntu:22.04"
label 'io_limited'
errorStrategy 'finish'
input:
file fasta_gz
output:
file "genes.shard.*.fasta.gz"
"""
#!/bin/bash
set -e
split --additional-suffix .fasta -l 1000000 <(gunzip -c ${fasta_gz}) genes.shard.
gzip genes.shard.*.fasta
"""
}
// Break out the annotation into a separate workflow
workflow Annotation_wf {
take:
gene_fasta
main:
// Split up the gene catalog into shards for more efficient processing
shard_genes(
gene_fasta
)
// Determine whether or not to run the eggNOG annotation based
// on --noannot and --eggnog_db / --eggnog_dmnd
run_eggnog = false
if ( params.noannot == false ) {
if ( params.eggnog_db && params.eggnog_dmnd ) {
eggnog_db = file(params.eggnog_db)
eggnog_dmnd = file(params.eggnog_dmnd)
if ( eggnog_db.isEmpty() ){
log.info"Cannot find file ${params.eggnog_db}, skipping functional annotation"
}
if ( eggnog_dmnd.isEmpty() ){
log.info"Cannot find file ${params.eggnog_dmnd}, skipping functional annotation"
}
if ( !eggnog_db.isEmpty() && !eggnog_dmnd.isEmpty() ){
run_eggnog = true
}
} else {
if ( params.eggnog_db ) {
log.info"Missing --eggnog_dmnd, skipping functional annotation"
}
if ( params.eggnog_dmnd ) {
log.info"Missing --eggnog_db, skipping functional annotation"
}
}
}
// Annotate the clustered genes with eggNOG
if ( run_eggnog ){
eggnog(
shard_genes.out.flatten(),
eggnog_db,
eggnog_dmnd
)
eggnog_tsv = eggnog.out.collect()
} else {
eggnog_tsv = false
}
// Determine whether or not to run the taxnomic annotation based
// on --noannot and --taxonomic_dmnd
run_tax = false
if ( params.noannot == false ) {
if ( params.taxonomic_dmnd ) {
taxonomic_dmnd = file(taxonomic_dmnd)
if ( !taxonomic_dmnd.isEmpty() ){
run_tax = true
} else {
log.info"Cannot find ${params.taxonomic_dmnd}, skipping taxonomic annotation"
}
}
}
// Annotate the clustered genes with DIAMOND for taxonomic ID
if ( run_tax ) {
diamond_tax(
shard_genes.out.flatten(),
taxonomic_dmnd
)
tax_tsv = diamond_tax.out.collect()
join_tax(
tax_tsv
)
} else {
tax_tsv = false
}
emit:
tax_tsv = tax_tsv
eggnog_tsv = eggnog_tsv
}
process diamond_tax {
tag "Annotate genes by taxonomy"
container "${container__diamond}"
label 'mem_veryhigh'
input:
file query
file diamond_tax_db
output:
file "genes.tax.aln.gz"
"""
set -e
diamond \
blastp \
--db ${diamond_tax_db} \
--query ${query} \
--out genes.tax.aln.gz \
--outfmt 102 \
--id ${params.min_identity} \
--top ${100 - params.min_identity} \
--block-size ${task.memory.toMega() / (1024 * 6)} \
--threads ${task.cpus} \
--compress 1
rm ${diamond_tax_db}
"""
}
process join_tax {
tag "Concatenate taxonomy annotation files"
container "ubuntu:22.04"
label 'io_limited'
publishDir "${params.output_folder}/annot/", mode: "copy"
input:
file "genes.tax.aln.*.gz"
output:
file "genes.tax.aln.gz"
"""
set -e
for fp in genes.tax.aln.*.gz; do
cat \$fp
rm \$fp
done > genes.tax.aln.gz
"""
}
process eggnog {
tag "Annotate genes by predicted function"
container "${container__eggnogmapper}"
label 'mem_veryhigh'
input:
path query
path eggnog_db
path eggnog_dmnd
output:
path "genes.emapper.annotations.gz"
"""
set -e
mkdir data
mkdir TEMP
mkdir SCRATCH
mv ${eggnog_db} data/eggnog.db
mv ${eggnog_dmnd} data/eggnog_proteins.dmnd
emapper.py \
-i ${query} \
--itype proteins \
--output genes \
-m "diamond" \
--cpu ${task.cpus} \
--data_dir data/ \
--scratch_dir SCRATCH/ \
--temp_dir TEMP/
gzip genes.emapper.annotations
"""
}
// Function which prints help message text
def helpMessage() {
log.info"""
Add functional and/or taxonomic annotations to a geneshot output file.
Usage:
nextflow run Golob-Minot/geneshot/modules/annotation <ARGUMENTS>
Options:
--gene_fasta Location for input 'genes.fasta.gz'
--output Location for output (default 'results')
--taxonomic_dmnd Database used for taxonomic annotation (default: false)
--ncbi_taxdump Reference describing the NCBI Taxonomy
(default: ftp://ftp.ncbi.nlm.nih.gov/pub/taxonomy/taxdump.tar.gz)
--eggnog_dmnd One of two databases used for functional annotation with eggNOG (default: false)
--eggnog_db One of two databases used for functional annotation with eggNOG (default: false)
####################################
# Downloading Reference Databases: #
####################################
--taxonomic_dmnd
The DIAMOND database of reference protein sequences must be indexed using both
(a) a set of sequences to search and (b) taxonomic annotations for each.
Full instructions for creating this indexed database file can be found
here: https://github.com/bbuchfink/diamond/wiki/3.-Command-line-options#makedb-options
Example:
diamond makedb \
--in <proteins_fasta> \
--db <output_dmnd> \
--taxonmap prot.accession2taxid.FULL.gz \
--taxonnodes nodes.dmp \
--taxonnames names.dmp
--eggnog_dmnd & --eggnog_db
The eggNOG database for functional annotation can be most easily downloaded
using the dedicated utility provided along with the eggNOG-mapper utility.
The only flag which needs to be set when running the download utility is the
destination folder for the downloaded files.
Example:
docker run -v \$PWD:/working ${container__eggnogmapper} download_eggnog_data.py -y --data_dir /working/
""".stripIndent()
}
workflow {
main:
// Show help message if the user specifies the --help flag at runtime
if (params.help || params.gene_fasta == false || params.output_folder == false){
// Invoke the function above which prints the help message
helpMessage()
// Exit out and do not run anything else
exit 0
}
// Show help message if the user does not specify any annotations
if (params.taxonomic_dmnd == false && params.eggnog_dmnd == false && params.eggnog_db == false){
// Invoke the function above which prints the help message
helpMessage()
// Exit out and do not run anything else
exit 0
}
// Make sure we can find the input files
if(file(params.gene_fasta).isEmpty()){
log.info"""Cannot find input file ${params.gene_fasta}""".stripIndent()
exit 0
}
// Run the annotation steps on the gene catalog
Annotation_wf(
file(params.gene_fasta)
)
}