Skip to content

Commit

Permalink
Fix: Threading errors due to database connection with NCBITaxa
Browse files Browse the repository at this point in the history
When the `NCBI` database connector is called from a different thread it
throws out a error preventing access to the database. The solution is to
create a new database connection for each `Lineage` object. I don't know
the potential memory or speed reductions but this works for now.
  • Loading branch information
dileep-kishore committed Feb 11, 2021
1 parent 5b76c1e commit e373c25
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions micone/main/lineage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

BaseLineage = namedtuple("Lineage", "Kingdom Phylum Class Order Family Genus Species")

if os.environ.get("RTD") != "True":
NCBI = NCBITaxa()
else:
NCBI = None


class Lineage(BaseLineage):
"""
Expand Down Expand Up @@ -54,6 +49,7 @@ def __new__(
)
)
norm_taxa = [cls._normalize_tax(i) for i in tax_order]
cls._ncbi = NCBITaxa()
return super().__new__(cls, *norm_taxa)

@staticmethod
Expand Down Expand Up @@ -241,7 +237,7 @@ def taxid(self) -> Tuple[str, int]:
query.append(query[-2] + " " + query[-1].strip())
# species level
query[-2] = query[-3] + " " + query[-2].split(" ")[0].strip()
taxid_dict = NCBI.get_name_translator(query)
taxid_dict = self._ncbi.get_name_translator(query)
taxid_list = [12908]
for taxa in reversed(query):
if taxa != "" and taxa in taxid_dict:
Expand Down Expand Up @@ -277,10 +273,11 @@ def from_taxid(cls, taxid: int) -> "Lineage":
"Lineage"
Instance of the `Lineage` class
"""
lineage_taxids = NCBI.get_lineage(taxid)
lineage_names = NCBI.get_taxid_translator(lineage_taxids)
ncbi = NCBITaxa()
lineage_taxids = ncbi.get_lineage(taxid)
lineage_names = ncbi.get_taxid_translator(lineage_taxids)
lineage_ranks = {
v.capitalize(): k for k, v in NCBI.get_rank(lineage_taxids).items()
v.capitalize(): k for k, v in ncbi.get_rank(lineage_taxids).items()
}
if "Superkingdom" in lineage_ranks:
lineage_ranks["Kingdom"] = lineage_ranks["Superkingdom"]
Expand Down

0 comments on commit e373c25

Please sign in to comment.