Skip to content

Commit

Permalink
improved type hints for lb score function
Browse files Browse the repository at this point in the history
  • Loading branch information
JLSteenwyk committed Sep 13, 2024
1 parent 271923a commit 23de937
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
57 changes: 30 additions & 27 deletions phykit/services/tree/lb_score.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import sys
import itertools
from typing import Dict, List, Tuple

from Bio.Phylo import Newick

from .base import Tree

Expand All @@ -26,28 +29,28 @@ def run(self):
stats = calculate_summary_statistics_from_arr(LBis)
print_summary_statistics(stats)

def process_args(self, args):
def process_args(self, args) -> Dict[str, str]:
return dict(tree_file_path=args.tree, verbose=args.verbose)

def calculate_average_distance_between_tips(self, tips: list, tree) -> float:
# determine pairwise combinations of tips
combos = list(itertools.combinations(tips, 2))
def calculate_average_distance_between_tips(
self,
tips: List[str],
tree: Newick.Tree,
) -> float:
total_dist = sum(
tree.distance(tip1, tip2)
for tip1, tip2 in itertools.combinations(tips, 2)
)

# determine average distance between tips
# avg_dist is PDa
total_dist = float()
for combo in combos:
total_dist += tree.distance(combo[0], combo[1])
num_combos = len(tips) * (len(tips) - 1) // 2

return total_dist / len(combos)
return total_dist / num_combos if num_combos else 0

def calculate_average_distance_of_taxon_to_other_taxa(
self, tips: list, tree
) -> list:
"""
calculate average distance of taxon to all other taxon or average PDi.
Save results to avg PDis list
"""
self,
tips: List[str],
tree: Newick.Tree,
) -> List[float]:
avg_PDis = []
for tip in tips:
tips_minus_i = list(set(tips) - set(tip))
Expand All @@ -59,10 +62,11 @@ def calculate_average_distance_of_taxon_to_other_taxa(

return avg_PDis

def calculate_lb_score_per_taxa(self, avg_PDis: list, avg_dist: float) -> list:
"""
create a list with the lb scores for each taxon
"""
def calculate_lb_score_per_taxa(
self,
avg_PDis: List[float],
avg_dist: float
) -> List[float]:
LBis = []
for PDi in avg_PDis:
try:
Expand All @@ -76,18 +80,17 @@ def calculate_lb_score_per_taxa(self, avg_PDis: list, avg_dist: float) -> list:

return LBis

def calculate_lb_score(self, tree):
# get tree tips
def calculate_lb_score(
self,
tree: Newick.Tree
) -> Tuple[List[str], List[float]]:
tips = self.get_tip_names_from_tree(tree)

# get average distance between tips
avg_dist = self.calculate_average_distance_between_tips(tips, tree)

# calculate average distance of taxon i to all other taxa
# or PDi and save each result to LBi
avg_PDis = self.calculate_average_distance_of_taxon_to_other_taxa(tips, tree)
avg_PDis = \
self.calculate_average_distance_of_taxon_to_other_taxa(tips, tree)

# use PDis and avgDist to calculate LB values for each taxon
LBis = self.calculate_lb_score_per_taxa(avg_PDis, avg_dist)

return tips, LBis
2 changes: 1 addition & 1 deletion tests/integration/tree/test_lb_score_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,4 @@ def test_lb_score_zero_division_error(self, mocked_print):

assert mocked_print.mock_calls == [
call("Invalid tree. Tree should contain branch lengths"),
]
]

0 comments on commit 23de937

Please sign in to comment.