diff --git a/metagenomics.py b/metagenomics.py index 2c5be61f..153b082f 100755 --- a/metagenomics.py +++ b/metagenomics.py @@ -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. diff --git a/test/unit/test_metagenomics.py b/test/unit/test_metagenomics.py index 1f42f9b3..f1798056 100644 --- a/test/unit/test_metagenomics.py +++ b/test/unit/test_metagenomics.py @@ -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})