How to use the newick.loads function in newick

To help you get started, we’ve selected a few newick examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github glottobank / python-newick / tests / test_newick.py View on Github external
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
github tskit-dev / msprime / tskit_tests / test_newick.py View on Github external
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)
github glottobank / python-newick / tests / test_newick.py View on Github external
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 == '((,(,)));'
github glottobank / python-newick / tests / test_newick.py View on Github external
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
github soedinglab / prosstt / prosstt / tree.py View on Github external
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
github soedinglab / prosstt / dev / tree.py View on Github external
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
github lmaurits / BEASTling / beastling / configuration.py View on Github external
# 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):

newick

A python module to read and write the Newick format

Apache-2.0
Latest version published 2 years ago

Package Health Score

54 / 100
Full package analysis

Similar packages