def test_adopt(self): """Adopt a node!""" n1 = TreeNode(name='n1') n2 = TreeNode(name='n2') n3 = TreeNode(name='n3') self.simple_t._adopt(n1) self.simple_t.children[-1]._adopt(n2) n2._adopt(n3) # adopt doesn't update .children self.assertEqual(len(self.simple_t.children), 2) self.assertIs(n1.parent, self.simple_t) self.assertIs(n2.parent, self.simple_t.children[-1]) self.assertIs(n3.parent, n2)
def test_gops(self): """Basic TreeNode operations should work as expected""" p = TreeNode() self.assertEqual(str(p), ';') p.name = 'abc' self.assertEqual(str(p), 'abc;') p.length = 3 self.assertEqual(str(p), 'abc:3;') # don't suppress branch from root q = TreeNode() p.append(q) self.assertEqual(str(p), '()abc:3;') r = TreeNode() q.append(r) self.assertEqual(str(p), '(())abc:3;') r.name = 'xyz' self.assertEqual(str(p), '((xyz))abc:3;') q.length = 2 self.assertEqual(str(p), '((xyz):2)abc:3;')
def test_siblings(self): """Get the siblings""" exp = [] obs = self.simple_t.siblings() self.assertEqual(obs, exp) exp = ['i2'] obs = self.simple_t.children[0].siblings() self.assertEqual([o.name for o in obs], exp) exp = ['c'] obs = self.simple_t.children[1].children[1].siblings() self.assertEqual([o.name for o in obs], exp) self.simple_t.append(TreeNode(name="foo")) self.simple_t.append(TreeNode(name="bar")) exp = ['i1', 'foo', 'bar'] obs = self.simple_t.children[1].siblings() self.assertEqual([o.name for o in obs], exp)
def test_missing_tip_name(self): """DndParser should produce the correct tree when missing a name""" obs = TreeNode.from_newick(missing_tip_name) exp = TreeNode() exp.append(TreeNode()) exp.append(TreeNode()) exp.children[0].append(TreeNode(name='a')) exp.children[0].append(TreeNode(name='b')) exp.children[1].append(TreeNode(name='c')) exp.children[1].append(TreeNode()) self.assertEqual(str(obs), str(exp))
def test_nonames(self): """DndParser should produce the correct tree when there are no names""" obs = TreeNode.from_newick(no_names) exp = TreeNode() exp.append(TreeNode()) exp.append(TreeNode()) exp.children[0].append(TreeNode()) exp.children[0].append(TreeNode()) exp.children[1].append(TreeNode()) exp.children[1].append(TreeNode()) self.assertEqual(str(obs), str(exp))
def setUp(self): """Prep the self""" self.simple_t = TreeNode.from_newick("((a,b)i1,(c,d)i2)root;") 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']) nodes['a'].append(nodes['h']) self.TreeNode = nodes self.TreeRoot = nodes['a']
def test_minimal(self): """DndParser should produce the correct minimal tree""" obs = TreeNode.from_newick(minimal) exp = TreeNode() exp.append(TreeNode()) self.assertEqual(str(obs), str(exp))
def test_remove(self): """Remove nodes""" self.assertTrue(self.simple_t.remove(self.simple_t.children[0])) self.assertEqual(len(self.simple_t.children), 1) n = TreeNode() self.assertFalse(self.simple_t.remove(n))