Skip to content

Commit

Permalink
Dependency and warning fix and compatibility with Scipy 1.12 (#122)
Browse files Browse the repository at this point in the history
* fix warning and some py3.10 incompatibilities

* format
  • Loading branch information
willdumm authored Jan 22, 2024
1 parent 23a7152 commit 889a653
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
12 changes: 7 additions & 5 deletions gctree/branching_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ def _simulate_genotype(p: np.float64, q: np.float64) -> Tuple[int, int]:
def _ll_genotype(
c: int, m: int, p: np.float64, q: np.float64
) -> Tuple[np.float64, np.ndarray]:
r"""Log-probability of getting :math:`c` leaves that are clones of the root and
:math:`m` mutant clades off the root lineage, given branching probability :math:`p` and
mutation probability :math:`q`.
r"""Log-probability of getting :math:`c` leaves that are clones of the
root and :math:`m` mutant clades off the root lineage, given branching
probability :math:`p` and mutation probability :math:`q`.
AKA the spaceship distribution. Also returns gradient wrt p and q
(p, q). Computed by dynamic programming.
Expand Down Expand Up @@ -1821,9 +1821,11 @@ def test_explode_individually():
if parent.label.sequence == node.label.sequence:
parent.label = node.label

if len(dag.hamming_parsimony_count()) > 1:
dag = hdag.sequence_dag.SequenceHistoryDag.from_history_dag(dag)
pars_count = dag.hamming_parsimony_count()
if len(pars_count) > 1:
raise RuntimeError(
f"History DAG parsimony search resulted in parsimony trees of unexpected weights:\n {dag.hamming_parsimony_count()}"
f"History DAG parsimony search resulted in parsimony trees of unexpected weights:\n {pars_count}"
)

# names on internal nodes are all messed up from disambiguation step, we'll
Expand Down
11 changes: 5 additions & 6 deletions gctree/mutation_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import numpy as np
from scipy.stats import poisson
import random
import scipy
from Bio.Seq import Seq
import historydag as hdag
from multiset import FrozenMultiset
Expand Down Expand Up @@ -134,7 +133,7 @@ def mutability(self, kmer: str) -> Tuple[np.float64, np.float64]:
*[self.context_model[x] for x in MutationModel._disambiguate(kmer)]
)

average_mutability = scipy.mean(mutabilities_to_average)
average_mutability = np.mean(mutabilities_to_average)
average_substitution = {
b: sum(
substitution_dict[b] for substitution_dict in substitutions_to_average
Expand Down Expand Up @@ -193,7 +192,7 @@ def mutate(
# number of mutations m
trials = 20
for trial in range(1, trials + 1):
m = scipy.random.poisson(lambda_sequence)
m = np.random.poisson(lambda_sequence)
if m <= sequence_length or self.with_replacement:
break
if trial == trials:
Expand All @@ -205,17 +204,17 @@ def mutate(
for i in range(m):
sequence_list = list(sequence) # make string a list so we can modify it
# Determine the position to mutate from the mutability matrix
mutability_p = scipy.array(
mutability_p = np.array(
[mutabilities[pos][0] for pos in unmutated_positions]
)
for trial in range(1, trials + 1):
mut_pos = scipy.random.choice(
mut_pos = np.random.choice(
unmutated_positions, p=mutability_p / mutability_p.sum()
)
# Now draw the target nucleotide using the substitution matrix
substitution_p = [mutabilities[mut_pos][1][n] for n in "ACGT"]
assert 0 <= abs(sum(substitution_p) - 1.0) < 1e-10
chosen_target = scipy.random.choice(4, p=substitution_p)
chosen_target = np.random.choice(4, p=substitution_p)
original_base = sequence_list[mut_pos]
sequence_list[mut_pos] = "ACGT"[chosen_target]
sequence = "".join(sequence_list) # reconstruct our sequence
Expand Down
2 changes: 1 addition & 1 deletion gctree/phylip_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def parse_outfile(outfile, abundance_file=None, root="root", disambiguate=False)
bootstrap = False
# Ugg... for compilation need to let python know that these will definely both be defined :-/
sequences, parents = {}, {}
with open(outfile, "rU") as fh:
with open(outfile, "r") as fh:
for sect in sections(fh):
if sect == "parents":
parents = {child: parent for child, parent in iter_edges(fh)}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
"seaborn",
"multiset",
"frozendict",
"historydag>=1"
"historydag>=1.2.0"
],
)

0 comments on commit 889a653

Please sign in to comment.