Skip to content

Commit 2a6bd91

Browse files
authored
MRG: make a clear error when acc cannot be found (#23)
* MRG: make a clear error when acc cannot be found * bump v * upd * refactor
1 parent 482f982 commit 2a6bd91

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "sourmash_plugin_pangenomics"
33
description = "sourmash plugin to do pangenomics."
44
readme = "README.md"
55
requires-python = ">=3.11"
6-
version = "0.3.0"
6+
version = "0.3.1"
77
authors = [
88
{name = "Colton Baumler", email = "ccbaumler@ucdavis.edu"},
99
{name = "Titus Brown", email = "titus@idyll.org"},

src/sourmash_plugin_pangenomics.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import os
1414
import re
1515
import pprint
16+
from difflib import get_close_matches
1617

1718
import sourmash
1819
import sourmash_utils
@@ -222,23 +223,27 @@ def pangenome_createdb_main(args):
222223
ident = tax_utils.get_ident(name)
223224

224225
# grab relevant lineage name
225-
try:
226-
lineage_tup = taxdb[ident]
227-
except KeyError: # older versions of genbank are not named with version!
228-
try:
229-
if "." in ident:
230-
short_ident = ident.split(".")[0]
231-
lineage_tup = taxdb[short_ident]
232-
else:
233-
for i in range(1, 10):
234-
try:
235-
new_ident = f"{ident}.{i}"
236-
lineage_tup = taxdb[new_ident]
237-
break
238-
except KeyError:
239-
continue
240-
except:
241-
print("Wow, that sucks!")
226+
lineage_tup = taxdb.get(ident)
227+
228+
# not found and has a .? maybe we can strip off the version.
229+
if lineage_tup is None and "." in ident:
230+
short_ident = ident.split(".")[0]
231+
lineage_tup = taxdb.get(ident)
232+
233+
# not found and has no .? Try many versions.
234+
if lineage_tup is None and "." not in ident:
235+
for i in range(1, 10):
236+
new_ident = f"{ident}.{i}"
237+
lineage_tup = taxdb.get(new_ident)
238+
if lineage_tup is not None:
239+
break
240+
241+
if lineage_tup is None:
242+
print(f"cannot find ident {ident} in the provided taxonomy ifle.")
243+
print(f"The three closest matches to {ident} are:")
244+
for k in get_close_matches(ident, taxdb):
245+
print(f"* '{k}'")
246+
sys.exit(-1)
242247

243248
lineage_tup = tax_utils.RankLineageInfo(lineage=lineage_tup)
244249
lineage_pair = lineage_tup.lineage_at_rank(args.rank)

0 commit comments

Comments
 (0)