Skip to content

Commit

Permalink
Allow fraction to be less than 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
apcamargo committed Sep 20, 2020
1 parent e88836c commit 2c94aed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

setup(
name="taxopy",
version="0.4.0",
version="0.4.1",
packages=find_packages(),
license="GNU General Public License v3.0",
description="A Python package for obtaining complete lineages and the lowest common ancestor (LCA) from a set of taxonomic identifiers.",
Expand Down
36 changes: 24 additions & 12 deletions taxopy/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def find_majority_vote(
A TaxDb object.
fraction: float, default 0.5
The returned taxon will be shared by more than `fraction` of the input
taxa lineages. This value must be equal to or greater than 0.5 and less
than 1.
taxa lineages. This value must be greater than 0.0 and less than 1.
weights: list, optional
A list of weights associated with the taxa lineages in `taxon_list`.
These values are used to weight the votes of their associated lineages.
Expand All @@ -86,17 +85,18 @@ def find_majority_vote(
-------
Taxon
The Taxon object of the most specific taxon that is shared by more than
half of the input lineages.
a specified fraction of the input lineages.
Raises
------
MajorityVoteError
If the input taxon list has less than two Taxon objects or if the
`fraction` parameter is less than 0.5 or equal to or greater than 1.
`fraction` parameter is less than or equal to 0.0 or greater than or
equal to 1.
"""
if fraction < 0.5 or fraction >= 1:
if fraction <= 0.0 or fraction >= 1:
raise MajorityVoteError(
"The `fraction` parameter must be equal to or greater than 0.5 and less than 1."
"The `fraction` parameter must be greater than 0.0 and less than 1."
)
if len(taxon_list) < 2:
raise MajorityVoteError(
Expand All @@ -117,13 +117,25 @@ def find_majority_vote(
majority_taxon[taxon] += weight
majority_taxon = sorted(
majority_taxon.items(), key=lambda x: x[1], reverse=True
)[0]
if majority_taxon[0] and majority_taxon[1] > total_weight * fraction:
return Taxon(majority_taxon[0], taxdb)
)
if majority_taxon[0][0] and majority_taxon[0][1] > total_weight * fraction:
if (
len(majority_taxon) > 1
and majority_taxon[0][1] > majority_taxon[1][1]
):
return Taxon(majority_taxon[0][0], taxdb)
elif len(majority_taxon) == 1:
return Taxon(majority_taxon[0][0], taxdb)
else:
n_taxa = len(taxon_list)
for taxonomic_level in reversed(zipped_taxid_lineage):
majority_taxon = Counter(taxonomic_level).most_common()[0]
if majority_taxon[0] and majority_taxon[1] > n_taxa * fraction:
return Taxon(majority_taxon[0], taxdb)
majority_taxon = Counter(taxonomic_level).most_common()
if majority_taxon[0][0] and majority_taxon[0][1] > n_taxa * fraction:
if (
len(majority_taxon) > 1
and majority_taxon[0][1] > majority_taxon[1][1]
):
return Taxon(majority_taxon[0][0], taxdb)
elif len(majority_taxon) == 1:
return Taxon(majority_taxon[0][0], taxdb)
return Taxon("1", taxdb)

0 comments on commit 2c94aed

Please sign in to comment.