How to use the cogent3.core.tree.TreeNode function in cogent3

To help you get started, we’ve selected a few cogent3 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 cogent3 / cogent3 / tests / test_core / test_tree.py View on Github external
def test_index_in_parent(self):
        """TreeNode index_in_parent should hold correct data"""
        first = TreeNode("a")
        second = TreeNode("b")
        third = TreeNode("c")
        fourth = TreeNode("0", children=[first, second, third])
        self.assertEqual(len(fourth), 3)
        self.assertEqual(first.index_in_parent(), 0)
        self.assertEqual(second.index_in_parent(), 1)
        self.assertEqual(third.index_in_parent(), 2)
        del fourth[0]
        self.assertEqual(second.index_in_parent(), 0)
        self.assertEqual(third.index_in_parent(), 1)
        self.assertEqual(len(fourth), 2)
        assert first.parent is None
github cogent3 / cogent3 / tests / test_core / test_tree.py View on Github external
def setUp(self):
        """Define some standard TreeNode for testing"""
        self.Empty = TreeNode()
        self.Single = TreeNode(name="a")
        self.Child = TreeNode(name="b")
        self.OneChild = TreeNode(name="a", children=[self.Child])
        self.Multi = TreeNode(name="a", children="bcd")
        self.Repeated = TreeNode(name="x", children="aaa")
        self.BigName = list(map(TreeNode, "0123456789"))
        self.BigParent = TreeNode(name="x", children=self.BigName)
        self.Comparisons = list(map(TreeNode, "aab"))

        nodes = dict([(x, TreeNode(x)) for x in "abcdefgh"])
        nodes["a"].append(nodes["b"])
        nodes["b"].append(nodes["c"])
        nodes["c"].append(nodes["d"])
        nodes["c"].append(nodes["e"])
        nodes["c"].append(nodes["f"])
        nodes["f"].append(nodes["g"])
github cogent3 / cogent3 / tests / test_core / test_tree.py View on Github external
def comb_tree(num_leaves):
    """Returns a comb node_class tree."""
    branch_child = 1

    root = TreeNode()
    curr = root

    for i in range(num_leaves - 1):
        curr.children[:] = [TreeNode(parent=curr), TreeNode(parent=curr)]
        curr = curr.children[branch_child]
    return root
github cogent3 / cogent3 / tests / test_core / test_tree.py View on Github external
def test_setitem(self):
        """TreeNode setitem should set item or extended slice of nodes"""
        parent, nodes = self.BigParent, self.BigName
        t = TreeNode(1)
        parent[0] = t
        assert parent[0] is t
        assert t.parent is parent
        assert nodes[0].parent is None

        u = TreeNode(2)
        parent[-2] = u
        assert parent[8] is u
        assert u.parent is parent
        assert nodes[8].parent is None

        parent[1:6:2] = "xyz"
        for i in [1, 3, 5]:
            assert nodes[i].parent is None
        self.assertEqual(parent[1].name, "x")
        self.assertEqual(parent[3].name, "y")
github cogent3 / cogent3 / tests / test_core / test_tree.py View on Github external
t3.name = "bar"

        self.assertEqual(len(t.tips()), 1024)
        self.assertEqual(len(t2.tips()), 1024)
        self.assertEqual(len(t3.tips()), 1024)

        self.assertNotSameObj(t, t2)
        self.assertEqual(t.name, t2.name)
        self.assertNotEqual(t.name, t3.name)

        self.assertEqual(t.XYZ, t2.XYZ)
        self.assertNotSameObj(t.XYZ, t2.XYZ)

        self.assertEqual(t.get_newick(), t2.get_newick())

        t_simple = TreeNode(["t"])
        u_simple = TreeNode(["u"])
        t_simple.append(u_simple)

        self.assertEqual(str(t_simple.copy()), str(t_simple.copy()))
github cogent3 / cogent3 / tests / test_core / test_tree.py View on Github external
def test_children(self):
        """TreeNode children should allow getting/setting children"""
        nodes = self.TreeNode
        for n in nodes:
            node = nodes[n]
            self.assertEqual(list(node), node.children)

        t = TreeNode(children="abc")
        self.assertEqual(len(t), 3)
        u, v = TreeNode("u"), TreeNode("v")

        # WARNING: If you set children directly, parent refs will _not_ update!
        t.children = [u, v]

        assert t[0] is u
        assert t[1] is v
        self.assertEqual(len(t), 2)
github cogent3 / cogent3 / tests / test_core / test_tree.py View on Github external
def test_append(self):
        """TreeNode append should add item to end of self"""
        self.OneChild.append(TreeNode("c"))
        self.assertEqual(len(self.OneChild), 2)
        self.assertEqual(self.OneChild[-1].name, "c")
        self.OneChild.append(6)
        self.assertEqual(len(self.OneChild), 3)
        self.assertEqual(self.OneChild[-1].name, 6)
        # check that refs are updated when moved from one tree to another
        empty = TreeNode()
        empty.append(self.OneChild[-1])
        self.assertEqual(len(empty), 1)
        self.assertEqual(empty[0].name, 6)
        self.assertEqual(empty[0].parent, empty)
        self.assertEqual(self.OneChild[-1].name, "c")
github cogent3 / cogent3 / tests / test_core / test_tree.py View on Github external
def test_eq(self):
        """TreeNode should compare equal if same id"""
        t, u, v = self.Comparisons
        self.assertEqual(t, t)
        assert t is not u
        self.assertNotEqual(t, u)
        self.assertNotEqual(t, v)

        f = TreeNode(1.0)
        g = TreeNode(1)
        self.assertNotEqual(f, g)
        f.name += 0.1
        self.assertNotEqual(f, g)

        # however, two TreeNodes that have no name should not compare equal
        f = TreeNode()
        g = TreeNode()
        self.assertNotEqual(f, g)

        f = TreeNode(name="foo")
        g = f.copy()
        self.assertNotEqual(f, g)
github cogent3 / cogent3 / tests / test_core / test_tree.py View on Github external
def test_index_in_parent(self):
        """TreeNode index_in_parent should hold correct data"""
        first = TreeNode("a")
        second = TreeNode("b")
        third = TreeNode("c")
        fourth = TreeNode("0", children=[first, second, third])
        self.assertEqual(len(fourth), 3)
        self.assertEqual(first.index_in_parent(), 0)
        self.assertEqual(second.index_in_parent(), 1)
        self.assertEqual(third.index_in_parent(), 2)
        del fourth[0]
        self.assertEqual(second.index_in_parent(), 0)
        self.assertEqual(third.index_in_parent(), 1)
        self.assertEqual(len(fourth), 2)
        assert first.parent is None
github cogent3 / cogent3 / src / cogent3 / app / evo.py View on Github external
"""
        super(natsel_sitehet, self).__init__(
            input_types=self._input_types,
            output_types=self._output_types,
            data_types=self._data_types,
        )
        self._formatted_params()
        if not is_codon_model(sm):
            raise ValueError(f"{sm} is not a codon model")

        if misc.path_exists(tree):
            tree = load_tree(filename=tree, underscore_unmunge=True)
        elif type(tree) == str:
            tree = make_tree(treestring=tree, underscore_unmunge=True)

        if tree and not isinstance(tree, TreeNode):
            raise TypeError(f"invalid tree type {type(tree)}")

        # instantiate model, ensuring genetic code setting passed on
        sm_args = sm_args or {}
        sm_args["gc"] = sm_args.get("gc", gc)
        sm_args["optimise_motif_probs"] = optimise_motif_probs
        if type(sm) == str:
            sm = get_model(sm, **sm_args)

        model_name = sm.name
        # defining the null model
        epsilon = 1e-6
        null_param_rules = [
            dict(par_name="omega", bins="-ve", upper=1 - epsilon, init=1 - epsilon),
            dict(par_name="omega", bins="neutral", is_constant=True, value=1.0),
        ]