Skip to content

Commit

Permalink
Update tax-tree to point to new api
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Falk committed Sep 20, 2020
1 parent d04afe2 commit 0ddd0ad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
4 changes: 3 additions & 1 deletion scripts/seqids_by_taxa.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def main():

# Go over all transcripts and assign the seqids (based on their taxid) to the group
# to which they belong, or the UNASSSIGNED group
count = 0
for gene in gene_data_report.genes:
taxgroup = taxmap.get(str(gene.tax_id), "")
for protein in gene.proteins:
Expand Down Expand Up @@ -80,7 +81,8 @@ def add_missing_taxids(taxtree, gene_data_report, email):
if not taxtree.get_org_if_exists(str(gene.tax_id)):
missing_taxa.add(str(gene.tax_id))
uids = ",".join(missing_taxa)
taxtree.add_entrez_taxa(uids, email)
if len(uids):
taxtree.add_entrez_taxa(uids, email)

if __name__ == "__main__":
main()
22 changes: 6 additions & 16 deletions scripts/tax_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,27 @@


'''
Construct a taxtree using the ncbi datasets TaxTreeApi service.
Construct a taxtree using the ncbi datasets GeneApi service.
Tree is constructed using a root tax id and tax info can be returned
based on tax-id.
Tree also supports separate lineage function and subtree function.
Examples:
taxtree = TaxTree(33208)
org = taxtree.get_org_if_exists("9606")
print("human: ", org)
lineage = taxtree.get_lineage("9606")
print("human lineage: ", lineage)
all_children = taxtree.get_all_children_for("207598")
print("children of 207598:", all_children)
'''
class TaxTree:
# defaults to metazoan
def __init__(self, tax_id=33208):
def __init__(self, tax_id="33208"):
self.node_map = {}
self.root = {}
with ncbi.datasets.ApiClient() as api_client:
api_instance = ncbi.datasets.TaxTreeApi(api_client)
# api_instance = ncbi.datasets.TaxTreeApi(api_client)
api_instance = ncbi.datasets.GeneApi(api_client)
try:
# Retrieve tax tree by taxonomy ID
api_response = api_instance.tax_tree_by_tax_id(tax_id)
api_response = api_instance.gene_tax_tree(tax_id)
tree = api_response.to_dict()
self.node_map = {}
self._build_node_map(tree)
self.root = self.node_map[str(tax_id)]
except ApiException as e:
logger.error(f"Exception when calling TaxTreeApi->tax_tree_by_tax_id: e")
logger.error(f"Exception when calling GeneApi->gene_tax_tree: e")

def _build_node_map(self, node):
self.node_map[node['tax_id']] = {
Expand All @@ -51,7 +42,6 @@ def _build_node_map(self, node):

if node['children']:
for org in node.get('children', []):
self.node_map[node['tax_id']]['children'].append(org['tax_id'])
self._build_node_map(org)

def add_tax_node(self, tax_id, tax_lineage, sci_name, common_name, rank):
Expand Down

0 comments on commit 0ddd0ad

Please sign in to comment.