Skip to content

Commit

Permalink
add get_ordered_ancestor method to TaxonomyDb
Browse files Browse the repository at this point in the history
  • Loading branch information
dpark01 committed Feb 8, 2024
1 parent a507039 commit 199ca36
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions metagenomics.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ def load_nodes(self, nodes_db):
ranks[taxid] = rank
return ranks, parents

def get_ordered_ancestors(self, taxid):
''' returns all ancestors of a taxid in proximity order: [parent, grandparent, greatgrandparent, etc] '''
if taxid in self.parents and taxid != self.parents[taxid]:
return [self.parents[taxid]] + self.get_ordered_ancestors(self.parents[taxid])
else:
return []

def process_blast_hits(self, hits, top_percent):
'''Filter groups of blast hits and perform lca.
Expand Down
4 changes: 4 additions & 0 deletions test/unit/test_metagenomics.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ def test_translate_gi_to_tax_id(taxa_db_simple):
assert taxa_db_simple.translate_gi_to_tax_id(blast1) == expected


def test_ancestor_lookup(taxa_db_simple):
assert taxa_db_simple.get_ordered_ancestors(4) == [3, 2, 1]


def test_kraken_dfs_report(taxa_db):
hits = Counter({1: 101, 3: 103, 10: 105, 12: 107})

Expand Down

0 comments on commit 199ca36

Please sign in to comment.