-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
163 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from typing import Generator, List | ||
from Bio import Entrez | ||
from tqdm import tqdm | ||
|
||
from pyeed.core.organism import Organism | ||
from pyeed.fetchers.abstractfetcher import AbstractFetcher | ||
from pyeed.fetchers.abstractfetcher import LOGGER | ||
|
||
|
||
class NCBITaxonomyParser(AbstractFetcher): | ||
|
||
def __init__(self, foreign_id: List[str], email: str = None, api_key: str = None): | ||
super().__init__(foreign_id) | ||
self.api_key = api_key | ||
if email is None: | ||
self.email = self.get_substitute_email() | ||
|
||
def get(self): | ||
|
||
if isinstance(self.foreign_id, list): | ||
return list(self.get_multiple_ids()) | ||
else: | ||
return list(self.get_single_id()) | ||
|
||
def make_request(self, request_string: str) -> Generator: | ||
Entrez.email = self.email | ||
Entrez.api_key = self.api_key | ||
|
||
with Entrez.efetch( | ||
db="taxonomy", | ||
id=request_string, | ||
retmode="xml", | ||
api_key=self.api_key, | ||
) as handle: | ||
yield handle | ||
|
||
def get_single_id(self): | ||
handle = next(self.make_request(self.foreign_id)) | ||
return Entrez.read(handle) | ||
|
||
def get_multiple_ids(self): | ||
with tqdm( | ||
total=len(self.foreign_id), | ||
desc="⬇️ Fetching taxonomy data", | ||
) as pbar: | ||
|
||
for chunk in self.make_chunks(self.foreign_id): | ||
request_string = ",".join(chunk) | ||
|
||
for handle in self.make_request(request_string): | ||
|
||
pbar.update(1) | ||
yield Entrez.read(handle) | ||
|
||
def map(self, cls: "Organism"): | ||
|
||
tax_id = self.source.get("TaxId") | ||
organism = cls(taxonomy_id=tax_id) | ||
|
||
organism.name = self.source.get("ScientificName") | ||
organism.species = self.source.get("ScientificName") | ||
|
||
lineage = self.source.get("LineageEx") | ||
|
||
if not lineage: | ||
LOGGER.debug(f"No lineage found for {tax_id}: {self.source}") | ||
return organism | ||
|
||
for tax_rank in lineage: | ||
if tax_rank.get("Rank") == "superkingdom": | ||
organism.domain = tax_rank.get("ScientificName") | ||
elif tax_rank.get("Rank") == "phylum": | ||
organism.phylum = tax_rank.get("ScientificName") | ||
elif tax_rank.get("Rank") == "class": | ||
organism.tax_class = tax_rank.get("ScientificName") | ||
elif tax_rank.get("Rank") == "order": | ||
organism.order = tax_rank.get("ScientificName") | ||
elif tax_rank.get("Rank") == "family": | ||
organism.family = tax_rank.get("ScientificName") | ||
elif tax_rank.get("Rank") == "genus": | ||
organism.genus = tax_rank.get("ScientificName") | ||
elif tax_rank.get("Rank") == "species": | ||
organism.species = tax_rank.get("ScientificName") | ||
elif tax_rank.get("Rank") == "kingdom": | ||
organism.kingdom = tax_rank.get("ScientificName") | ||
else: | ||
continue | ||
|
||
return organism | ||
|
||
|
||
if __name__ == "__main__": | ||
single_tax_id = "9606" | ||
multiple_tax_ids = ["9606"] | ||
|
||
# print(NCBITaxonomyParser(single_tax_id).get()) | ||
|
||
print(NCBITaxonomyParser(multiple_tax_ids).get()) |
Empty file.
Oops, something went wrong.