Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_scoped_local(self):
model = cogent3.evolve.substitution_model.TimeReversibleNucleotide(
equal_motif_probs=True, model_gaps=True, predicates={"kappa": "transition"}
)
lf = model.make_likelihood_function(self.tree)
lf.set_constant_lengths()
lf.set_alignment(self.al)
null = lf.get_num_free_params()
lf.set_param_rule(par_name="kappa", is_independent=True, edges=["b", "d"])
self.assertEqual(null + 2, lf.get_num_free_params())
probabilities"""
aln_len = len(self.aln)
posn1 = []
posn2 = []
for name, seq in list(self.aln.to_dict().items()):
p1 = [seq[i] for i in range(0, aln_len, 2)]
p2 = [seq[i] for i in range(1, aln_len, 2)]
posn1.append([name, "".join(p1)])
posn2.append([name, "".join(p2)])
# the position specific alignments
posn1 = make_aligned_seqs(data=posn1)
posn2 = make_aligned_seqs(data=posn2)
# a newQ dinucleotide model
sm = TimeReversibleNucleotide(motif_length=2, mprob_model="monomer")
lf = sm.make_likelihood_function(self.tree)
lf.set_alignment(posn1)
posn1_lnL = lf.get_log_likelihood()
lf.set_alignment(posn2)
posn2_lnL = lf.get_log_likelihood()
expect_lnL = posn1_lnL + posn2_lnL
# the joint model
lf.set_alignment(self.aln)
aln_lnL = lf.get_log_likelihood()
# setting the full alignment, which has different motif probs, should
# produce a different lnL
self.assertNotAlmostEqual(aln_lnL, expect_lnL)
# set the arguments for taking position specific mprobs
def _makeModel(self, predicates, scale_rules=None):
scale_rules = scale_rules or []
return substitution_model.TimeReversibleNucleotide(
equal_motif_probs=True,
model_gaps=False,
predicates=predicates,
scales=scale_rules,
)
def compare_models(motif_probs, motif_length):
# if the 1st and 2nd position motifs are independent of each other
# then conditional is the same as positional
ps = TimeReversibleNucleotide(
motif_length=motif_length,
motif_probs=motif_probs,
mprob_model="monomers",
)
cd = TimeReversibleNucleotide(
motif_length=motif_length,
motif_probs=motif_probs,
mprob_model="conditional",
)
ps_lf = ps.make_likelihood_function(self.tree)
ps_lf.set_param_rule("length", is_independent=False, init=0.4)
ps_lf.set_alignment(self.aln)
cd_lf = cd.make_likelihood_function(self.tree)
cd_lf.set_param_rule("length", is_independent=False, init=0.4)
def test_get_param_list(self):
"""testing getting the parameter list"""
model = substitution_model.TimeReversibleNucleotide()
self.assertEqual(model.get_param_list(), [])
model = substitution_model.TimeReversibleNucleotide(
predicates=["beta:transition"]
)
self.assertEqual(model.get_param_list(), ["beta"])
def test_results_different(self):
for (i, (mprobs, dummy)) in enumerate(self.ordered_by_complexity):
results = []
for (dummy, model) in self.ordered_by_complexity:
di = TimeReversibleNucleotide(
motif_length=2, motif_probs=mprobs, mprob_model=model
)
lf = di.make_likelihood_function(self.tree)
lf.set_param_rule("length", is_independent=False, init=0.4)
lf.set_alignment(self.aln)
lh = lf.get_log_likelihood()
for other in results[:i]:
self.assertNotAlmostEqual(other, lh, places=2)
for other in results[i:]:
self.assertFloatEqual(other, lh)
results.append(lh)
def getsubmod(self, choice="F81"):
if choice == "F81":
return substitution_model.TimeReversibleNucleotide(model_gaps=True)
else:
return substitution_model.TimeReversibleNucleotide(
model_gaps=True, predicates={"kappa": "transition"}
)
def test_get_statistics(self):
"""get statistics should correctly apply arguments"""
for (mprobs, model) in self.ordered_by_complexity:
di = TimeReversibleNucleotide(
motif_length=2, motif_probs=mprobs, mprob_model=model
)
lf = di.make_likelihood_function(self.tree)
for wm, wt in [(True, True), (True, False), (False, True), (False, False)]:
stats = lf.get_statistics(with_motif_probs=wm, with_titles=wt)
def GTR(**kw):
"""General Time Reversible nucleotide substitution model."""
required = dict(
name="GTR", predicates=_gtr_preds, mprob_model="conditional", model_gaps=False
)
kwargs = dict(recode_gaps=True, motif_probs=None)
kwargs.update(kw)
kwargs.update(required)
return substitution_model.TimeReversibleNucleotide(**kwargs)