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_limited_wls(self):
"""testing (well, exercising at least), wls with constrained start"""
init = make_tree(treestring="((a,c),b,d)")
reconstructed = wls(self.dists, start=init, show_progress=False)
self.assertEqual(len(reconstructed.get_tip_names()), 6)
init2 = make_tree(treestring="((a,d),b,c)")
reconstructed = wls(self.dists, start=[init, init2], show_progress=False)
self.assertEqual(len(reconstructed.get_tip_names()), 6)
init3 = make_tree(treestring="((a,d),b,z)")
self.assertRaises(Exception, wls, self.dists, start=[init, init3])
# if start tree has all seq names, should raise an error
self.assertRaises(
Exception,
wls,
self.dists,
start=[make_tree(treestring="((a,c),b,(d,(e,f)))")],
)
def test_get_connecting_edges(self):
"""correctly identify connecting edges"""
tree = make_tree(treestring="(((Human,HowlerMon)a,Mouse)b,NineBande,DogFaced);")
edges = [e.name for e in tree.get_connecting_edges("Human", "Mouse")]
self.assertEqual(set(edges), set(["Human", "Mouse", "a"]))
edges = [e.name for e in tree.get_connecting_edges("b", "Human")]
self.assertEqual(set(edges), set(["Human", "a", "b"]))
def test_length_attr_valid(self):
"""Tests whether setting a custom length attribute provides valid x values"""
tree = make_tree(
treestring="((a:0.1,b:0.25):0.1,(c:0.02,d:0.03, (e:0.035, f:0.04):0.15):0.3 , g:0.3)"
)
geom = SquareTreeGeometry(tree, length_attr="custom")
geom.params["custom"] = 1
for e in geom.preorder():
if e.is_root():
continue
e.params["custom"] = e.parent.params.get("custom", 1) * 2
geom.propagate_properties()
# .x attribute is cumulative from the root, which we have set to 1
# for 'custom', e.g. a.x == 2 + 4 == 6
func = geom.get_node_matching_name
actual_vals = [
func("root").x,
treestring = (
"((Homo sapiens:2,(Mus_musculus_129S1SvImJ:1,"
"(Mus_musculus:0.1,Mus_musculus_LPJ:0.2):0.3)Mus_musculus:0.3)"
",Canis_familiaris,Ornithorhynchus_anatinus)"
)
tree = make_tree(treestring=treestring, underscore_unmunge=True)
# change internal node names to eliminate ending digits
for edge in tree.postorder():
if edge.istip():
continue
if "Mus" in edge.name:
edge.name = "Mus musculus"
sub1 = tree.get_sub_tree(names, tipsonly=False)
self.assertTrue(tree.same_topology(sub1))
expect = make_tree(
treestring="(Homo_sapiens,Mus_musculus,"
"(Canis_familiaris,Ornithorhynchus_anatinus))",
underscore_unmunge=True,
)
sub2 = tree.get_sub_tree(names, tipsonly=True)
self.assertTrue(expect.same_topology(sub2))
def _maketree(self, treestring=None):
if treestring is None:
treestring = self.default_newick
return make_tree(treestring=treestring, underscore_unmunge=True)
def test_balanced(self):
"""balancing an unrooted tree"""
t = make_tree(treestring="((a,b),((c1,(c2,(c3,(c4,(c5,(c6,c7)))))),(d,e)),f)")
b = make_tree(treestring="(c1,(c2,(c3,(c4,(c5,(c6,c7))))),((d,e),((a,b),f)))")
self.assertEqual(str(t.balanced()), str(b))
def test_initialise_from_nested_diff_scoped(self):
"""non-reversible likelihood initialised from nested, scoped, time-reversible"""
mprobs = {b: p for b, p in zip(DNA, [0.1, 0.2, 0.3, 0.4])}
rate_params = {"A/C": 2.0, "A/G": 3.0, "A/T": 4.0, "C/G": 5.0, "C/T": 6.0}
rate_params1 = {"A/C": 4.0, "A/G": 6.0, "C/T": 3.0}
simple = GTR()
tree = make_tree(tip_names=["Human", "Mouse", "Opossum"])
slf = simple.make_likelihood_function(tree, digits=2)
slf.set_alignment(_aln)
slf.set_name("GTR")
slf.set_motif_probs(mprobs)
for param, val in rate_params.items():
slf.set_param_rule(param, init=val)
for param, val in rate_params1.items():
slf.set_param_rule(param, init=val, edges=["Human"])
lengths = {e: v for e, v in zip(tree.get_tip_names(), (0.2, 0.4, 0.1))}
for e, val in lengths.items():
slf.set_param_rule("length", edge=e, init=val)
rich = GN(optimise_motif_probs=True)
glf = rich.make_likelihood_function(tree, digits=2)
def test_to_rich_dict(self):
"""lf's from different substitution model classes can make rich dict"""
# mixture of discrete-time, continuous-time models
names = ["BH", "DT", "CNFGTR", "GN", "WG01"]
tree = make_tree(tip_names=_aln.names)
for name in names:
sm = get_model(name)
lf = sm.make_likelihood_function(tree)
lf.set_alignment(_aln)
_ = lf.to_rich_dict()
def _est_dist_pair_slow(self, aln):
"""returns distance between seq pairs in aln"""
assert len(aln.names) == 2
tree = make_tree(tip_names=aln.names)
lf = self._sm.make_likelihood_function(tree)
lf.set_alignment(aln)
lf.set_param_rule("length", is_independent=False)
lf.optimise(max_restarts=0, show_progress=False)
dist = 2 * lf.get_param_value("length", edge=aln.names[0])
return dist
def _doset(self, sequence_names, dist_opt_args, aln_opt_args, ui):
# slice the alignment
seqs = self._seq_collection.take_seqs(sequence_names)
if self._do_pair_align:
ui.display("Aligning", progress=0.0)
align = self._make_pair_alignment(seqs, aln_opt_args)
ui.display("", progress=0.5)
else:
align = seqs
ui.display("", progress=0.0)
# note that we may want to consider removing the redundant gaps
# create the tree object
tree = make_tree(tip_names=sequence_names)
# make the parameter controller
lf = self._sm.make_likelihood_function(tree)
if not self._threeway:
lf.set_param_rule("length", is_independent=False)
if self._motif_probs:
lf.set_motif_probs(self._motif_probs)
lf.set_alignment(align)
# allow user modification of lf using the modify_lf
if self._modify_lf:
lf = self._modify_lf(lf)
lf.optimise(**dist_opt_args)