forked from proksee-project/ProkaML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_mapping.py
377 lines (258 loc) · 13.6 KB
/
build_mapping.py
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
"""
Copyright Government of Canada 2021
Written by:
Eric Marinier
National Microbiology Laboratory, Public Health Agency of Canada
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this work except in compliance with the License. You may obtain a copy of the
License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
"""
This script builds a tab-separated mapping file to be used with ONE corresponding Mash sketch file. The script
maps NCBI accession IDs found in the 'query comment' field of the output of 'mash info' (of a Mash sketch file)
to their NCBI taxonomy ID, their ranked lineage, and their full lineage.
The mapping file will have a format similar to the following:
[Accession ID] [Taxonomy ID] [Ranked Lineage (multiple columns)] [Full Name Lineage (one column)]
NC_005100 10116 Rattus norvegicus Rattus Muridae Rodentia [...] Organisms; Eukaryota; [...]
The following files will be required:
mash_info: the output from running 'mash info' on a Mash sketch; the file produced by this program is
expected to work ONLY with this Mash sketch file
ranked_lineage: a NCBI file mapping NCBI taxonomy IDs to their 'ranked' lineage
full_name_lineage: a NCBI file mapping NCBI taxonomy IDs to their 'full' lineage
merged: a NCBI file mapping old NCBI accession IDs to their new, merged NCBI accession IDs
(multiple) mapping files: a list of NCBI files mapping NCBI accession IDs to NCBI taxonomy IDs
As of 2021-06-23, the following files were used when building a mapping file:
- mash_info: output of 'mash info -t' on this sketch: https://gembox.cbcb.umd.edu/mash/refseq.genomes.k21s1000.msh
- ranked_lineage: rankedlineage.dmp in https://ftp.ncbi.nih.gov/pub/taxonomy/new_taxdump/new_taxdump.tar.gz
- full_name_lineage: fullnamelineage.dmp in https://ftp.ncbi.nih.gov/pub/taxonomy/new_taxdump/new_taxdump.tar.gz
- merged: merged.dmp in https://ftp.ncbi.nih.gov/pub/taxonomy/new_taxdump/new_taxdump.tar.gz
mapping files:
- https://ftp.ncbi.nih.gov/pub/taxonomy/accession2taxid/dead_wgs.accession2taxid.gz
- https://ftp.ncbi.nih.gov/pub/taxonomy/accession2taxid/dead_nucl.accession2taxid.gz
- https://ftp.ncbi.nih.gov/pub/taxonomy/accession2taxid/nucl_gb.accession2taxid.gz
- https://ftp.ncbi.nih.gov/pub/taxonomy/accession2taxid/nucl_wgs.accession2taxid.gz
"""
import sys
import argparse
MASH_INFO_FILENAME = "mash_info_filename"
RANKED_LINEAGE_FILENAME = "ranked_lineage_filename"
FULL_NAME_LINEAGE_FILENAME = "full_name_lineage_filename"
MERGED_FILENAME = "merged_filename"
MAPPING_FILENAMES = "mapping_filenames"
def build_mapping_file():
"""
Builds a tab-separated mapping file. The file maps NCBI accession IDs to its associated tax ID
and taxonomy lineage information.
POST
If successful, tab-separated information mapping:
[(Old/New) NCBI Accession ID] -> [TaxID, Ranked Lineage, Full Name Lineage]
will be written to standard output. For example:
[Accession ID] [Taxonomy ID] [Ranked Lineage (multiple columns)] [Full Name Lineage (one column)]
NC_005100 10116 Rattus norvegicus Rattus Muridae Rodentia [...] Organisms; Eukaryota; [...]
"""
# Argument parsing:
args = parse_arguments()
parameters = vars(args)
mash_info_filename = parameters.get(MASH_INFO_FILENAME)
ranked_lineage_filename = parameters.get(RANKED_LINEAGE_FILENAME)
full_name_lineage_filename = parameters.get(FULL_NAME_LINEAGE_FILENAME)
merged_filename = parameters.get(MERGED_FILENAME)
mapping_filenames = parameters.get(MAPPING_FILENAMES)
# Build dictionary mapping [NCBI Accession ID] -> [NCBI Taxonomy ID]
accessions = get_accession_IDs(mash_info_filename)
add_taxonomy_to_accessions(accessions, mapping_filenames)
# Build dictionary mapping [NCBI Taxnomy ID] -> [Ranked Lineage, Full Name Lineage]
lineages = map_taxonomy_to_lineage(ranked_lineage_filename)
append_full_name_lineage(lineages, full_name_lineage_filename)
# Build dictionary mapping [Old NCBI Accession ID] -> [New NCBI Accession ID]
# This is because some accession IDs are no longer used.
merged_ids = build_merged_IDs(merged_filename)
# Output the mapping [(Old/New) NCBI Accession ID] -> [TaxID, Ranked Lineage, Full Name Lineage]
output_mapping(accessions, lineages, merged_ids)
def parse_arguments():
"""
Parses the command-line arguments are returns a populated Namespace object with argument strings as
attributes.
RETURNS:
args (argparse.Namespace): an Namespace object with argument strings as attributes
"""
parser = argparse.ArgumentParser(description='Builds an NCBI ID-to-taxonomy mapping file.')
parser.add_argument('-mi', dest=MASH_INFO_FILENAME, type=str, required=True,
help='The output of "mash info" for the Mash sketch file.')
parser.add_argument('-r', dest=RANKED_LINEAGE_FILENAME, type=str, required=True,
help='NCBI ranked lineage file mapping IDs to lineages.')
parser.add_argument('-f', dest=FULL_NAME_LINEAGE_FILENAME, type=str, required=True,
help='NCBI full name lineage file mapping IDs to full name lineages.')
parser.add_argument('-m', dest=MERGED_FILENAME, type=str, required=True,
help='NCBI file mapping old IDs to new, merged IDs.')
parser.add_argument('-maps', dest=MAPPING_FILENAMES, type=str, required=True, nargs='+',
help='NCBI files mapping Accession IDs to taxonomy IDs.')
args = parser.parse_args()
return args
def get_accession_IDs(mash_info_filename):
"""
Get all NCBI accession IDs that are observed in the output of 'mash info -t'. The format of the 'mash info' file
needs to be very particular:
1000 143726002 GCF_000001215.4_Release_6_plus_ISO1_MT_genomic.fna.gz [1870 seqs] NC_004354.4 Drosophila [...]
That is, the accession needs to appear in the 'query comment' field of 'mash info' output. It may optionally be preceeded
by a "[# seqs]" substring.
PARAMETERS:
mash_info_filename (str): the name of the file containing the output of 'mash_info -t'
RETURNS:
accessions (dict(str->None)): a dictionary containing only NCBI accession IDs as keys, all mapping to None
"""
QUERY_COMMENT = 3
accessions = {}
with open(mash_info_filename) as f:
next(f) # Skip header
for line in f:
line = line.strip()
tokens = line.split("\t")
string = tokens[QUERY_COMMENT]
# Check for optional "[# seqs]":
if string.startswith("["):
string = string.split("] ")[1]
# Grab the Accession ID:
accession = string.split(" ")[0]
accession = accession.split(".")[0] # Remove the version (NC_000001.1 -> NC_000001)
accessions[accession] = None
return accessions
def add_taxonomy_to_accessions(accessions, mapping_filenames):
"""
Adds NCBI taxonomy IDs to a NCBI accession IDs dictionary by iterating over several files that map accession IDs
to taxonomy IDs and adding matches when found.
PARAMETERS:
accessions (dict(str->None)): a dictionary with NCBI accession IDs as keys; this dictionary will be editted
POST
The 'accessions' dictionary will be modified to have taxonomy IDs added as values for each accession ID key.
"""
ACCESSION = 0
TAXID = 2
for filename in mapping_filenames:
with open(filename) as f:
next(f) # skip header
for line in f:
tokens = line.split()
accession = tokens[ACCESSION]
taxid = tokens[TAXID]
if accession in accessions:
accessions[accession] = taxid
def map_taxonomy_to_lineage(ranked_lineage_filename):
"""
Maps taxonomy IDs to ranked lineage information. In particular, builds a dictionary of the following form:
[Taxonomy ID] -> [Taxonomy Name, Species, Genus, Family, Order, Class, Phylum, Kingdom, Superkingdom]
PARAMETERS:
ranked_lineage_filename (str): the name of the file mapping NCBI taxonomy IDs to their full ranked lineage
RETURNS:
lineages (dict(int->list(str))): a dictionary mapping taxonmy IDs to ranked lineage information
"""
TAXID = 0
TAX_NAME = 1
SPECIES = 2
GENUS = 3
FAMILY = 4
ORDER = 5
CLASS = 6
PHYLUM = 7
KINGDOM = 8
SUPERKINGDOM = 9
lineages = {}
with open(ranked_lineage_filename) as f:
for line in f:
tokens = line.split("|")
taxid = tokens[TAXID].strip()
tax_name = tokens[TAX_NAME].strip() if len(tokens[TAX_NAME].strip()) > 0 else "-"
species = tokens[SPECIES].strip() if len(tokens[SPECIES].strip()) > 0 else "-"
genus = tokens[GENUS].strip() if len(tokens[GENUS].strip()) > 0 else "-"
family = tokens[FAMILY].strip() if len(tokens[FAMILY].strip()) > 0 else "-"
order = tokens[ORDER].strip() if len(tokens[ORDER].strip()) > 0 else "-"
clas = tokens[CLASS].strip() if len(tokens[CLASS].strip()) > 0 else "-"
phylum = tokens[PHYLUM].strip() if len(tokens[PHYLUM].strip()) > 0 else "-"
kingdom = tokens[KINGDOM].strip() if len(tokens[KINGDOM].strip()) > 0 else "-"
superkingdom = tokens[SUPERKINGDOM].strip() if len(tokens[SUPERKINGDOM].strip()) > 0 else "-"
lineages[taxid] = [tax_name, species, genus, family, order, clas, phylum, kingdom, superkingdom]
return lineages
def append_full_name_lineage(lineages, full_name_lineage_filename):
"""
Appends the 'full name lineage' to the lineage dictionary mapping taxonomy IDs to ranked lineage information. The
dictionary will have the following form after complection:
[Taxonomy ID] -> [Taxonomy Name, Species, Genus, Family, Order, Class, Phylum, Kingdom, Superkingdom, Full Name]
The 'full name lineage' tends to look like the following:
"cellular organisms; Eukaryota; Opisthokonta; Metazoa; [...]; Monotremata; Ornithorhynchidae; Ornithorhynchus"
PARAMETERS:
lineages (dict(int->list(str))): a dictionary mapping taxonmy IDs to ranked lineage information; this dictionary
will be modified after execution
full_name_lineage_filename (str): the name of the file containing taxonomy ID to full name lineage mapping
information
POST
The 'lineages' parameter will have full name lineages appended to the lineage information for each taxonomy ID.
"""
TAXID = 0
FULL_LINEAGE = 2
with open(full_name_lineage_filename) as f:
for line in f:
tokens = line.split("|")
taxid = tokens[TAXID].strip()
full_lineage = tokens[FULL_LINEAGE].strip()
full_lineage = full_lineage[:-1] # remove the trailing ";" character
lineages[taxid].append(full_lineage)
def build_merged_IDs(merged_filename):
"""
Builds a dictionary mapping old, deprecated NCBI accession IDs to new NCBI accession IDs. This is (usually?) because
the old IDs have been merged together into a new ID.
PARAMETERS:
merged_filename (str): the name of the file mapping old NCBI accession IDs to new NCBI accession IDs
RETURN:
merged_ids (dict(str->str)): a dictionary mapping old IDs to new IDs
"""
OLD_ID = 0
NEW_ID = 1
merged_ids = {}
with open(merged_filename) as f:
for line in f:
tokens = line.split("|")
old_id = tokens[OLD_ID].strip()
new_id = tokens[NEW_ID].strip()
merged_ids[old_id] = new_id
return merged_ids
def output_mapping(accessions, lineages, merged_ids):
"""
Iterates through various dictionaries to output, line-by-line, and writes the mapping of NCBI accession ID to
its associated taxonomic lineage information.
PARAMETERS:
accessions (dict(str->str)): dictionary mapping NCBI accession IDs to NCBI taxonomy IDs
lineages (dict(int->list(str))): dictionary mapping taxonomy IDs to ranked and full lineage information
merged_ids (dict(str->str)): dictionary mapping old NCBI accession IDs to new NCBI accession IDs
POST
If successful, tab-separated information mapping:
[(Old/New) NCBI Accession ID] -> [TaxID, Ranked Lineage, Full Name Lineage]
will be written to standard output. For example:
[Accession ID] [Taxonomy ID] [Ranked Lineage] [Full Name Lineage]
NC_005100 10116 Rattus norvegicus Rattus Muridae Rodentia [...] Organisms; Eukaryota; [...]
"""
for accession in accessions:
taxid = accessions[accession]
if taxid in lineages:
lineage = lineages[taxid]
output = ""
output += accession + "\t"
output += taxid + "\t"
output += "\t".join(lineage)
print(output)
elif taxid in merged_ids:
new_id = merged_ids[taxid]
lineage = lineages[new_id]
output = ""
output += accession + "\t"
output += taxid + "\t"
output += "\t".join(lineage)
print(output)
else:
sys.stderr.write("The following TaxID is missing: " + str(taxid) + "\n")
# Run the program:
build_mapping_file()