Skip to content

Commit

Permalink
Add __eq__ to Taxon
Browse files Browse the repository at this point in the history
  • Loading branch information
apcamargo committed Aug 4, 2022
1 parent 0917b72 commit ee051c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import taxopy

First you need to download taxonomic information from NCBI's servers and put this data into a `TaxDb` object:


```python
taxdb = taxopy.TaxDb()
# You can also use your own set of taxonomy files:
Expand All @@ -37,7 +36,6 @@ taxdb = taxopy.TaxDb(nodes_dmp="taxdb/nodes.dmp", names_dmp="taxdb/names.dmp", m

The `TaxDb` object stores the name, rank and parent-child relationships of each taxonomic identifier:


```python
print(taxdb.taxid2name[2])
print(taxdb.taxid2parent[2])
Expand All @@ -48,20 +46,16 @@ print(taxdb.taxid2rank[2])
131567
superkingdom


If you want to retrieve the new taxonomic identifier of a legacy identifier you can use the `oldtaxid2newtaxid` attribute:


```python
print(taxdb.oldtaxid2newtaxid[260])
```

143224


To get information of a given taxon you can create a `Taxon` object using its taxonomic identifier:


```python
saccharomyces = taxopy.Taxon(4930, taxdb)
human = taxopy.Taxon(9606, taxdb)
Expand All @@ -71,7 +65,6 @@ lagomorpha = taxopy.Taxon(9975, taxdb)

Each `Taxon` object stores a variety of information, such as the rank, identifier and name of the input taxon, and the identifiers and names of all the parent taxa:


```python
print(lagomorpha.rank)
print(lagomorpha.name)
Expand Down Expand Up @@ -99,18 +92,15 @@ print(lagomorpha_parent.name)

You can get the lowest common ancestor of a list of taxa using the `find_lca` function:


```python
human_lagomorpha_lca = taxopy.find_lca([human, lagomorpha], taxdb)
print(human_lagomorpha_lca.name)
```

Euarchontoglires


You may also use the `find_majority_vote` to discover the most specific taxon that is shared by more than half of the lineages of a list of taxa:


```python
majority_vote = taxopy.find_majority_vote([human, gorilla, lagomorpha], taxdb)
print(majority_vote.name)
Expand Down
23 changes: 14 additions & 9 deletions taxopy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,6 @@ def parent(self, taxdb) -> Taxon:
parent_taxid = taxdb.taxid2parent[self.taxid]
return Taxon(parent_taxid, taxdb)

def __str__(self) -> str:
lineage = [
f"{rank[0]}__{name}" for rank, name in self.rank_name_dictionary.items()
]
return ";".join(reversed(lineage))

def __repr__(self) -> str:
return str(self)

def _find_lineage(self, taxid2parent):
current_taxid = self.taxid
lineage = [current_taxid]
Expand All @@ -352,6 +343,20 @@ def _convert_to_rank_dictionary(self, taxid2rank, taxid2name):
rank_name_dictionary[rank] = taxid2name[taxid]
return rank_taxid_dictionary, rank_name_dictionary

def __str__(self) -> str:
lineage = [
f"{rank[0]}__{name}" for rank, name in self.rank_name_dictionary.items()
]
return ";".join(reversed(lineage))

def __repr__(self) -> str:
return str(self)

def __eq__(self, other: object) -> bool:
if other.__class__ is not self.__class__:
return NotImplemented
return self.taxid_lineage == other.taxid_lineage


class _AggregatedTaxon(Taxon):
"""
Expand Down

0 comments on commit ee051c9

Please sign in to comment.