Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert len(root.descendants) == 3
root = loads('(A,B,(C,D));')[0]
assert root.name is None
assert len(root.descendants) == 3
root = loads('(A,B,(C,D)E)Fäß;')[0]
assert root.name == 'Fäß'
assert len(root.descendants) == 3
root = loads('(:0.1,:0.2,(:0.3,:0.4):0.5);')[0]
assert root.name is None
assert root.descendants[0].length == 0.1
assert len(root.descendants) == 3
root = loads('((B:0.2,(C:0.3,D:0.4)E:0.5)F:0.1)A;')[0]
assert root.name == 'A'
assert root.descendants[-1].length == 0.1
assert len(root.descendants) == 1
def verify_newick_topology(self, tree, root=None, node_labels=None):
if root is None:
root = tree.root
ns = tree.newick(precision=16, root=root, node_labels=node_labels)
if node_labels is None:
leaf_labels = {u: str(u + 1) for u in tree.leaves(root)}
else:
leaf_labels = {u: node_labels[u] for u in tree.leaves(root)}
newick_tree = newick.loads(ns)[0]
leaf_names = newick_tree.get_leaf_names()
self.assertEqual(sorted(leaf_names), sorted(leaf_labels.values()))
for u in tree.leaves(root):
name = leaf_labels[u]
node = newick_tree.get_node(name)
while u != root:
self.assertAlmostEqual(node.length, tree.branch_length(u))
node = node.ancestor
u = tree.parent(u)
self.assertIsNone(node.ancestor)
def test_all_removal():
tree = loads('((B:0.2,(C:0.3,D:0.4)E:0.5)F:0.1)A;')[0]
tree.remove_names()
tree.remove_lengths()
topology_only = dumps(tree)
assert topology_only == '((,(,)));'
def test_polytomy_resolution():
tree = loads('(A,B,(C,D,(E,F)))')[0]
assert not tree.is_binary
tree.resolve_polytomies()
assert tree.newick == '(A,((C,((E,F),D):0.0),B):0.0)'
assert tree.is_binary
tree = loads('(A,B,C,D,E,F)')[0]
assert not tree.is_binary
tree.resolve_polytomies()
assert tree.newick == '(A,(F,(B,(E,(C,D):0.0):0.0):0.0):0.0)'
assert tree.is_binary
def from_newick(cls, newick_tree,
modules=None,
genes=def_genes,
density=None):
"""
Generate a lineage tree from a Newick-formatted string.
"""
tree = newick.loads(newick_tree)
top, time, branches, br_points, root = tu.parse_newick(tree, cls.def_time)
tree = Tree(top, time, branches, br_points, modules, genes, density, root)
return tree
def from_newick(cls, newick_tree,
modules=None,
genes=def_genes,
density=None):
"""
Generate a lineage tree from a Newick-formatted string.
"""
tree = newick.loads(newick_tree)
top, time, branches, br_points, root = tu.parse_newick(tree, cls.def_time)
tree = Tree(top, time, branches, br_points, modules, genes, density, root)
return tree
# user did not expect to get here. They might want
# this to be an originate cal, or a tip cal, but we
# can't tell with what we know and shouldn't
# guess. Abort and skip to the next cal. This should
# never happen, because empty calibrations can only be
# specified by empty language groups, which should be
# caught before this.
self.messages.append("[INFO] Calibration on clade '%s' matches only one language. Ignoring due to ambiguity. Use 'originate(%s)' if this was supposed to be an originate calibration, or explicitly identify the single language using '%s' if this was supposed to be a tip calibration." % (clade, clade, langs[0]))
continue
# Make sure this calibration point, which will induce a monophyly
# constraint, does not conflict with the overall monophyly
# constraints from Glottolog or a user-tree
if self.languages.monophyly and len(langs) > 1:
mono_tree = newick.loads(self.languages.monophyly_newick)[0]
cal_clade = set(langs)
for node in mono_tree.walk():
mono_clade = set(node.get_leaf_names())
# If the calibration clade is not a subset of this monophyly clade, keep searching
if not cal_clade.issubset(mono_clade):
continue
# At this point, we can take it for granted the cal clade is a subset of the mono_clade
# We are happy if the calibration clade is exactly this monophyly clade
if mono_clade == cal_clade:
break
# We are also happy if this mono_clade is a "terminal clade", i.e. has no finer structure
# which the calibration clade may violate
elif all((child.is_leaf for child in node.descendants)):
break
# We are also happy if the calibration clade is a union of descendant mono clades
elif all(set(child.get_leaf_names()).issubset(cal_clade) or len(set(child.get_leaf_names()).intersection(cal_clade)) == 0 for child in node.descendants):