-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed implementation to avoid the use of apc
- Loading branch information
1 parent
b8b9631
commit b942e61
Showing
1 changed file
with
12 additions
and
19 deletions.
There are no files selected for viewing
31 changes: 12 additions & 19 deletions
31
hstrat/_auxiliary_lib/_alifestd_calc_triplet_distance_asexual.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,28 @@ | ||
import alifedata_phyloinformatics_convert as apc | ||
import pandas as pd | ||
import tqdist | ||
|
||
from . import ( | ||
alifestd_as_newick_asexual, | ||
alifestd_collapse_unifurcations, | ||
) | ||
|
||
|
||
# adapted from https://github.com/mmore500/hstrat/blob/d23917cf03ba59061ff2f9b951efe79e995eb4d8/tests/test_hstrat/test_phylogenetic_inference/test_tree/_impl/_tree_quartet_distance.py | ||
def alifestd_calc_triplet_distance_asexual( | ||
ref: pd.DataFrame, cmp: pd.DataFrame | ||
) -> float: | ||
"""Calculate the triplet distance between two trees.""" | ||
tree_a = apc.RosettaTree(ref).as_dendropy | ||
tree_b = apc.RosettaTree(cmp).as_dendropy | ||
|
||
# must suppress root unifurcations or tqdist barfs | ||
# see https://github.com/uym2/tripVote/issues/15 | ||
tree_a.unassign_taxa(exclude_leaves=True) | ||
tree_a.suppress_unifurcations() | ||
tree_b.unassign_taxa(exclude_leaves=True) | ||
tree_b.suppress_unifurcations() | ||
ref = alifestd_collapse_unifurcations(ref) | ||
cmp = alifestd_collapse_unifurcations(cmp) | ||
|
||
tree_a_taxon_labels = [ | ||
leaf.taxon.label for leaf in tree_a.leaf_node_iter() | ||
] | ||
tree_b_taxon_labels = [ | ||
leaf.taxon.label for leaf in tree_b.leaf_node_iter() | ||
] | ||
assert {*tree_a_taxon_labels} == {*tree_b_taxon_labels} | ||
for taxon_label in tree_a_taxon_labels: | ||
ref_labels = {*ref["taxon_label"]} | ||
assert ref_labels == {*cmp["taxon_label"]} | ||
for taxon_label in ref_labels: | ||
assert taxon_label | ||
assert taxon_label.strip() | ||
|
||
return tqdist.triplet_distance( | ||
tree_a.as_string(schema="newick").removeprefix("[&R]").strip(), | ||
tree_b.as_string(schema="newick").removeprefix("[&R]").strip(), | ||
alifestd_as_newick_asexual(ref).removeprefix("[&R]").strip(), | ||
alifestd_as_newick_asexual(cmp).removeprefix("[&R]").strip(), | ||
) |