Skip to content

Commit

Permalink
added type hints and cleaned up the tbs function
Browse files Browse the repository at this point in the history
  • Loading branch information
JLSteenwyk committed Sep 15, 2024
1 parent 4e35562 commit 3753f2f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
42 changes: 30 additions & 12 deletions phykit/services/tree/terminal_branch_stats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import sys
from typing import Dict, List, Tuple, Union

from Bio.Phylo import Newick

from .base import Tree

Expand All @@ -14,7 +17,8 @@ def __init__(self, args) -> None:

def run(self):
tree = self.read_tree_file()
_, stats, lengths_and_names = self.calculate_terminal_branch_stats(tree)
_, stats, lengths_and_names = \
self.calculate_terminal_branch_stats(tree)

if self.verbose:
try:
Expand All @@ -25,17 +29,25 @@ def run(self):
else:
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 get_terminal_branch_lengths(self, tree) -> list:
def get_terminal_branch_lengths(
self,
tree: Newick.Tree,
) -> List[
Union[
float,
List[List[Union[float, str]]],
]
]:
"""
loop through tree and get all terminal branch lengths
"""
terminal_branch_lengths = []
lengths_and_names = []
for terminal_branch in tree.get_terminals():
if terminal_branch.branch_length != None:
if terminal_branch.branch_length is not None:
temp = []
temp.append(terminal_branch.branch_length)
terminal_branch_lengths.append(terminal_branch.branch_length)
Expand All @@ -44,7 +56,10 @@ def get_terminal_branch_lengths(self, tree) -> list:

return terminal_branch_lengths, lengths_and_names

def check_tree_has_branch_lengths(self, terminal_branch_lengths: list) -> None:
def check_tree_has_branch_lengths(
self,
terminal_branch_lengths: List[float],
) -> None:
"""
if tree has no branch lengths, exit
"""
Expand All @@ -54,16 +69,19 @@ def check_tree_has_branch_lengths(self, terminal_branch_lengths: list) -> None:
)
sys.exit()

def calculate_terminal_branch_stats(self, tree):
# save terminal branch lengths to terminal_branch_lengths
terminal_branch_lengths, lengths_and_names = self.get_terminal_branch_lengths(
tree
)
def calculate_terminal_branch_stats(
self,
tree: Newick.Tree,
) -> Tuple[
List[float],
Dict[str, float],
List[List[Union[float, str]]],
]:
terminal_branch_lengths, lengths_and_names = \
self.get_terminal_branch_lengths(tree)

# If the phylogeny had no branch lengths, inform user and quit
self.check_tree_has_branch_lengths(terminal_branch_lengths)

# calculate summary stats
stats = calculate_summary_statistics_from_arr(terminal_branch_lengths)

return terminal_branch_lengths, stats, lengths_and_names
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import pytest
import sys
from math import isclose
from mock import patch, call
from pathlib import Path
from textwrap import dedent
import pytest
import sys

from phykit.phykit import Phykit

Expand Down Expand Up @@ -132,4 +130,4 @@ def test_terminal_branch_stats_alias(self, mocked_print):
Phykit()

assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 2
assert pytest_wrapped_e.value.code == 2

0 comments on commit 3753f2f

Please sign in to comment.