Skip to content

Commit

Permalink
Add rank_lineage, ranked_name_lineage, and ranked_taxid_lineage
Browse files Browse the repository at this point in the history
  • Loading branch information
apcamargo committed Sep 4, 2023
1 parent 437f94b commit 55c76d2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ Each `Taxon` object stores a variety of information, such as the rank, identifie
print(lagomorpha.rank)
print(lagomorpha.name)
print(lagomorpha.name_lineage)
print(lagomorpha.ranked_name_lineage)
print(lagomorpha.rank_name_dictionary)
```

order
Lagomorpha
['Lagomorpha', 'Glires', 'Euarchontoglires', 'Boreoeutheria', 'Eutheria', 'Theria', 'Mammalia', 'Amniota', 'Tetrapoda', 'Dipnotetrapodomorpha', 'Sarcopterygii', 'Euteleostomi', 'Teleostomi', 'Gnathostomata', 'Vertebrata', 'Craniata', 'Chordata', 'Deuterostomia', 'Bilateria', 'Eumetazoa', 'Metazoa', 'Opisthokonta', 'Eukaryota', 'cellular organisms', 'root']
[('order', 'Lagomorpha'), ('clade', 'Glires'), ('superorder', 'Euarchontoglires'), ('clade', 'Boreoeutheria'), ('clade', 'Eutheria'), ('clade', 'Theria'), ('class', 'Mammalia'), ('clade', 'Amniota'), ('clade', 'Tetrapoda'), ('clade', 'Dipnotetrapodomorpha'), ('superclass', 'Sarcopterygii'), ('clade', 'Euteleostomi'), ('clade', 'Teleostomi'), ('clade', 'Gnathostomata'), ('clade', 'Vertebrata'), ('subphylum', 'Craniata'), ('phylum', 'Chordata'), ('clade', 'Deuterostomia'), ('clade', 'Bilateria'), ('clade', 'Eumetazoa'), ('kingdom', 'Metazoa'), ('clade', 'Opisthokonta'), ('superkingdom', 'Eukaryota'), ('no rank', 'cellular organisms'), ('no rank', 'root')]
{'order': 'Lagomorpha', 'clade': 'Opisthokonta', 'superorder': 'Euarchontoglires', 'class': 'Mammalia', 'superclass': 'Sarcopterygii', 'subphylum': 'Craniata', 'phylum': 'Chordata', 'kingdom': 'Metazoa', 'superkingdom': 'Eukaryota'}

You can use the `parent` method to get a `Taxon` object of the parent node of a given taxon:
Expand Down
37 changes: 36 additions & 1 deletion taxopy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import tarfile
import urllib.request
from collections import OrderedDict
from typing import Dict, List
from typing import Dict, List, Tuple

from taxopy.exceptions import DownloadError, ExtractionError, TaxidError

Expand Down Expand Up @@ -249,6 +249,17 @@ class Taxon:
name_lineage: list
An ordered list containing the names of the whole lineage of the taxon,
from the most specific to the most general.
rank_lineage: list
An ordered list containing the rank names of the whole lineage of the
taxon, from the most specific to the most general.
ranked_name_lineage : list
An ordered list of tuples, where each tuple represents a rank in the
lineage, with the first element denoting the rank name and the second
indicating the taxon's name.
ranked_taxid_lineage : list
An ordered list of tuples, where each tuple represents a rank in the
lineage, with the first element denoting the rank name and the second
indicating the taxon's taxonomic identifier.
rank_taxid_dictionary: dict
A dictionary where the keys are named ranks and the values are the taxids
of the taxa that correspond to each of the named ranks in the lineage.
Expand Down Expand Up @@ -281,6 +292,7 @@ def __init__(self, taxid: int, taxdb: TaxDb):
self._legacy_taxid = None
self._taxid_lineage = self._find_lineage(taxdb.taxid2parent)
self._name_lineage = self._convert_to_names(taxdb.taxid2name)
self._rank_lineage = [taxdb._taxid2rank[taxid] for taxid in self.taxid_lineage]
(
self._rank_taxid_dictionary,
self._rank_name_dictionary,
Expand Down Expand Up @@ -310,6 +322,18 @@ def taxid_lineage(self) -> List[int]:
def name_lineage(self) -> List[str]:
return self._name_lineage

@property
def rank_lineage(self) -> List[str]:
return self._rank_lineage

@property
def ranked_taxid_lineage(self) -> List[Tuple[str, int]]:
return list(zip(self.rank_lineage, self.taxid_lineage))

@property
def ranked_name_lineage(self) -> List[Tuple[str, str]]:
return list(zip(self.rank_lineage, self.name_lineage))

@property
def rank_taxid_dictionary(self) -> Dict[str, int]:
return self._rank_taxid_dictionary
Expand Down Expand Up @@ -393,6 +417,17 @@ class _AggregatedTaxon(Taxon):
name_lineage: list
An ordered list containing the names of the whole lineage of the taxon,
from the most specific to the most general.
rank_lineage: list
An ordered list containing the rank names of the whole lineage of the
taxon, from the most specific to the most general.
ranked_name_lineage : list
An ordered list of tuples, where each tuple represents a rank in the
lineage, with the first element denoting the rank name and the second
indicating the taxon's name.
ranked_taxid_lineage : list
An ordered list of tuples, where each tuple represents a rank in the
lineage, with the first element denoting the rank name and the second
indicating the taxon's taxonomic identifier.
rank_taxid_dictionary: dict
A dictionary where the keys are named ranks and the values are the taxids
of the taxa that correspond to each of the named ranks in the lineage.
Expand Down

0 comments on commit 55c76d2

Please sign in to comment.