/
gene_abund.nf
363 lines (269 loc) · 9.05 KB
/
gene_abund.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
#!/usr/bin/env nextflow
/*
Geneshot: A pipeline to robustly identify which alleles (n.e.e peptide coding sequences)
are present in a microbial community.
This utility extracts the proportion of gene copies from each specimen which are
annotated with a given function by the eggNOG-mapper functional annotation tool.
To use this utility, provide a set of previously-generated geneshot results to query,
as well as a string which will be used to select all eggNOG annotations which contain it.
*/
// Using DSL-2
nextflow.preview.dsl=2
// Parameters
params.results_hdf = false
params.details_hdf = false
params.genes_fasta = false
params.output_folder = false
params.output_prefix = false
params.query = false
params.help = false
// Function which prints help message text
def helpMessage() {
log.info"""
This utility extracts the proportion of gene copies from each specimen which are
annotated with a given function by the eggNOG-mapper functional annotation tool.
To use this utility, provide a set of previously-generated geneshot results to query,
as well as a string which will be used to select all eggNOG annotations which contain it.
Usage:
nextflow run Golob-Minot/geneshot/gene_abund.nf <ARGUMENTS>
Options:
--results_hdf Location for results.hdf5 generated by geneshot
--details_hdf Location for details.hdf5 generated by geneshot
--genes_fasta Location for input 'genes.fasta.gz'
--output_folder Location for output files
--output_prefix Prefix for output files
--query Query string to use to subset eggNOG gene descriptions
""".stripIndent()
}
// Show help message if the user specifies the --help flag at runtime
if (params.help || params.results_hdf == false || params.details_hdf == false || params.genes_fasta == false || params.output_prefix == false || params.output_folder == false || params.query == false){
// Invoke the function above which prints the help message
helpMessage()
// Exit out and do not run anything else
exit 0
}
workflow {
// Make sure we can find the input files
if(file(params.results_hdf).isEmpty()){
log.info"""Cannot find input file ${params.results_hdf}""".stripIndent()
exit 0
}
if(file(params.details_hdf).isEmpty()){
log.info"""Cannot find input file ${params.details_hdf}""".stripIndent()
exit 0
}
if(file(params.genes_fasta).isEmpty()){
log.info"""Cannot find input file ${params.genes_fasta}""".stripIndent()
exit 0
}
// Get the table of genes which contain this annotation
extractAnnotations(
file(params.results_hdf)
)
// Get the sequences of the genes which match this query
extractFASTA(
extractAnnotations.out,
file(params.genes_fasta)
)
// Get the user-provided manifest
extractManifest(
file(params.results_hdf)
)
// Get the proportion of gene copies for these genes across all specimens
extractAbund(
extractAnnotations.out,
file(params.details_hdf),
extractManifest.out
)
}
process extractAnnotations {
container "quay.io/fhcrc-microbiome/integrate-metagenomic-assemblies:v0.5"
label "mem_medium"
input:
file results_hdf
output:
file "${params.output_prefix}.genes.csv"
publishDir "${params.output_folder}", mode: 'copy', overwrite: true
"""#!/usr/bin/env python3
import os
import pandas as pd
# Set the input path
results_hdf = '${results_hdf}'
# Make sure that the file is present in the working folder
assert os.path.exists(results_hdf)
# Set up a function to filter a DataFrame by the query string
query_str = '${params.query}'
def filter_df(df):
return df.loc[
df['eggNOG_desc'].fillna('').apply(lambda s: query_str in s)
]
# Read in the table in chunks and filter as we go
df = pd.concat([
filter_df(chunk_df)
for chunk_df in pd.read_hdf(
results_hdf,
'/annot/gene/all',
iterator=True
)
])
print("Number of genes containing the query '%s': %d" % (query_str, df.shape[0]))
# If there are any genes matching this string
if df.shape[0] > 0:
# Write out the smaller table
df.to_csv("${params.output_prefix}.genes.csv", index=None)
print("Done")
else:
print("NO GENES FOUND MATCHING THE QUERY: %s" % query_str)
"""
}
process extractManifest {
container "quay.io/fhcrc-microbiome/integrate-metagenomic-assemblies:v0.5"
label "mem_medium"
input:
file results_hdf
output:
file "${params.output_prefix}.manifest.csv"
publishDir "${params.output_folder}", mode: 'copy', overwrite: true
"""#!/usr/bin/env python3
import os
import pandas as pd
# Set the input path
results_hdf = '${results_hdf}'
# Make sure that the file is present in the working folder
assert os.path.exists(results_hdf)
# Read the manifest
manifest_df = pd.read_hdf(results_hdf, "/manifest")
# Write out to a file
manifest_df.to_csv("${params.output_prefix}.manifest.csv", index=None)
"""
}
process extractFASTA {
container "quay.io/fhcrc-microbiome/integrate-metagenomic-assemblies:v0.5"
label "io_limited"
input:
file gene_csv
file gene_fasta_gz
output:
file "${params.output_prefix}.genes.fasta.gz"
publishDir "${params.output_folder}", mode: 'copy', overwrite: true
"""#!/usr/bin/env python3
from Bio.SeqIO.FastaIO import SimpleFastaParser
import gzip
import os
import pandas as pd
# Set the input paths
# Make sure that the files are present in the working folder
gene_csv = '${gene_csv}'
assert os.path.exists(gene_csv)
gene_fasta_gz = '${gene_fasta_gz}'
assert os.path.exists(gene_fasta_gz)
output_fasta_gz = '${params.output_prefix}.genes.fasta.gz'
assert not os.path.exists(output_fasta_gz)
# Read in the table
df = pd.read_csv(gene_csv)
print("Read in annotations for %d genes" % df.shape[0])
# Get the list of genes
gene_names = set(df['gene'].tolist())
# Keep a counter of how many genes we've found
n_found = 0
# Open the input and output files
with gzip.open(gene_fasta_gz, 'rt') as handle_in, gzip.open(output_fasta_gz, 'wt') as handle_out:
# Iterate over the inputs
for gene_name, gene_seq in SimpleFastaParser(handle_in):
# If this is one of the genes we are looking for
if gene_name in gene_names:
# Write it out
handle_out.write(">%s\\n%s\\n" % (gene_name, gene_seq))
# Increment the counter
n_found += 1
# Report the number of genes which were found
print("Wrote out %d gene sequences" % n_found)
print("DONE")
"""
}
process extractAbund {
container "quay.io/fhcrc-microbiome/integrate-metagenomic-assemblies:v0.5"
label "mem_medium"
input:
file gene_csv
file details_hdf
file manifest_csv
output:
file "${params.output_prefix}.*.csv.gz"
publishDir "${params.output_folder}", mode: 'copy', overwrite: true
"""#!/usr/bin/env python3
import os
import pandas as pd
# Set the input paths
# Make sure that the files are present in the working folder
gene_csv = '${gene_csv}'
assert os.path.exists(gene_csv)
details_hdf = '${details_hdf}'
assert os.path.exists(details_hdf)
manifest_csv = '${manifest_csv}'
assert os.path.exists(manifest_csv)
# Read in the table of gene annotations
df = pd.read_csv(gene_csv)
print("Read in annotations for %d genes" % df.shape[0])
# Get the list of genes
gene_names = set(df['gene'].tolist())
# Read in the manifest
manifest_df = pd.read_csv(manifest_csv)
# Keep the complete set of gene abundances in long format
output = []
# Open a connection to the HDF store
with pd.HDFStore(details_hdf, 'r') as store:
# Set up an object to save the proportion of gene copies in each specimen
for specimen_name in manifest_df['specimen'].unique():
print("Reading in abundances for specimen '%s'" % specimen_name)
# Read in the full table
specimen_df = pd.read_hdf(store, "/abund/gene/long/%s" % specimen_name)
# Get the total depth for all gene copies
tot = specimen_df['depth'].sum()
# Subset to the genes of interest, add the specimen, add the proportion of gene copies, and append to the output
output.append(
specimen_df.loc[
specimen_df['id'].isin(gene_names)
].assign(
specimen = specimen_name,
prop = lambda d: d['depth'] / tot
)
)
# Combine all of the output
print("Combining all outputs")
output = pd.concat(
output
).reset_index(
drop=True
)
# Add the eggNOG annotation
print("Adding eggNOG names")
output = output.assign(
eggNOG_desc = output['id'].apply(
df.set_index('gene')['eggNOG_desc'].get
)
)
# Save the long output
print("Saving long output")
output.to_csv(
"${params.output_prefix}.long.csv.gz",
index=None,
compression='gzip'
)
# Save the wide output
output.pivot_table(
index="specimen",
columns="eggNOG_desc",
values="prop",
aggfunc=sum
).fillna(
0
).reset_index(
).to_csv(
"${params.output_prefix}.wide.csv.gz",
index=None,
compression='gzip'
)
print("DONE")
"""
}