Skip to content

Commit

Permalink
cleaned up evolutionary rate analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
JLSteenwyk committed Sep 13, 2024
1 parent 1bbe34f commit a3ebe2a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 80 deletions.
70 changes: 2 additions & 68 deletions phykit/services/tree/dvmc.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,5 @@
# import math
# from typing import Tuple

# from .base import Tree


# class DVMC(Tree):
# def __init__(self, args) -> None:
# super().__init__(**self.process_args(args))

# def run(self):
# tree = self.read_tree_file()
# dvmc = self.determine_dvmc(tree)

# print(round(dvmc, 4))

# def process_args(self, args):
# return dict(tree_file_path=args.tree)

# def get_term_to_root_dist_and_sum_of_distances(self, tree) -> Tuple[float, float]:
# """
# calculate root to tip distances and
# the sum of root to tip distances
# """
# dist = []
# sum_dist = 0
# for term in tree.get_terminals():
# # append root to tip distance to dist
# dist.append((tree.distance(term)))
# # keep running sum of tree distances
# sum_dist += tree.distance(term)

# return dist, sum_dist

# def calculate_dvmc(self, dist: float, sum_dist: float, num_spp: int) -> float:
# """
# calculate dvmc from tip to root distances
# """
# # determine dvmc
# # calculate average tree distance
# avg_dist = sum_dist / num_spp

# # determine the sum of i=1 to N for (x_i-x_bar)^2
# sumi2N = float(0.0)
# for x_i in dist:
# sumi2N += (x_i - avg_dist) ** 2

# # multiply sumi2N by 1/(N-1) where N is the number of spp
# # and then take the square root
# dvmc = float(0.0)
# dvmc = math.sqrt((1 / (num_spp - 1)) * sumi2N)

# return dvmc

# def determine_dvmc(self, tree: Tree):
# # loop through terminal branches and store
# # distances from the root to the tip in a list.
# # Also, calc the sum of all tip to root distances
# dist, sum_dist = self.get_term_to_root_dist_and_sum_of_distances(tree)

# # determine number of taxa in the tree
# num_spp = tree.count_terminals()

# # calculate and return dvmc
# return self.calculate_dvmc(dist, sum_dist, num_spp)


import math
from typing import Dict

from Bio.Phylo import Newick

Expand All @@ -81,7 +15,7 @@ def run(self):
dvmc = self.determine_dvmc(tree)
print(round(dvmc, 4))

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

def determine_dvmc(self, tree: Newick.Tree) -> float:
Expand Down
15 changes: 5 additions & 10 deletions phykit/services/tree/evolutionary_rate.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
from typing import Dict

from .base import Tree


class EvolutionaryRate(Tree):
def __init__(self, args) -> None:
super().__init__(**self.process_args(args))

def run(self):
def run(self) -> None:
tree = self.read_tree_file()
total_tree_length = self.calculate_total_tree_length(tree)
total_tree_length = tree.total_branch_length()
num_terminals = tree.count_terminals()
print(round(total_tree_length / num_terminals, 4))

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

def calculate_total_tree_length(self, tree):
total_len = float(0.0)
total_len = tree.total_branch_length()

if isinstance(total_len, (int, float)):
return total_len
2 changes: 0 additions & 2 deletions tests/integration/tree/test_evolutionary_rate.py
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

from phykit.phykit import Phykit

Expand Down

0 comments on commit a3ebe2a

Please sign in to comment.