def test_addChildren(self): """RangeNode add_children should add specified # children to list""" t = RangeNode() t2 = RangeNode(Parent=t) t.addChildren(5) self.assertEqual(len(t.Children), 6) assert t.Children[0] is t2 for c in t.Children: assert c.Parent is t
def __init__(self, Length=0, Array=None, *args, **kwargs): """Returns new MicroarrayNode object. Length: float giving the branch length (sd to add to data) Array: array of float giving the expression vector, or None Additional args for superclass: Name: usually a text label giving the name of the node LeafRange: range of leaves that the node spans Id: unique numeric identifier from the node Children: list of Node objects specifying the children Parent: Node object specifying the parent """ RangeNode.__init__(self, *args, **kwargs) self.Length = Length self.Array = Array
def test_init(self): """Make sure keyword arguments are being passed to baseclass""" node = RangeNode(LeafRange=1, Id=2, Name='foo', Length=42) self.assertEqual(node.LeafRange, 1) self.assertEqual(node.Id, 2) self.assertEqual(node.Name, 'foo') self.assertEqual(node.Length, 42)
def test_str(self): """RangeNode should round-trip Newick string corrrectly.""" r = RangeNode() self.assertEqual(str(r), '()') #should work for tree with branch lengths set t = DndParser(self.sample_tree_string, RangeNode) expected = self.sample_tree_string.replace('\n', '') expected = expected.replace(' ', '') self.assertEqual(str(t), expected) #self.assertEqual(t.getNewick(with_distances=True), expected) #should also work for tree w/o branch lengths t2 = DndParser(self.sample_string_2, RangeNode) self.assertEqual(str(t2), self.sample_string_2)
def test_fromBreakpoints(self): """RangeNode fromBreakpoints should have correct topology""" breakpoints = [4, 2, 1, 0, 3, 6, 5] t = RangeNode.fromBreakpoints(breakpoints) #check number of leaves self.assertEqual(len(list(t.traverse())), 8) self.assertEqual(len(list(t.traverse(self_before=True))), 15) #check that leaves were created in right order self.assertEqual([i.Id for i in t.traverse()], range(8)) #check that whole toplogy is right wrt ids... nodes = list(t.traverse(self_before=True)) obs = [i.Id for i in nodes] exp = [8, 9, 11, 13, 0, 1, 2, 12, 3, 4, 10, 14, 5, 6, 7] self.assertEqual(obs, exp) #...and ranges obs = [i.LeafRange for i in nodes] exp = [(0,8),(0,5),(0,3),(0,2),(0,1),(1,2),(2,3),(3,5),(3,4),(4,5), \ (5,8),(5,7),(5,6),(6,7),(7,8)] self.assertEqual(obs, exp)
def test_fromBreakpoints(self): """RangeNode fromBreakpoints should have correct topology""" breakpoints = [4,2,1,0,3,6,5] t = RangeNode.fromBreakpoints(breakpoints) #check number of leaves self.assertEqual(len(list(t.traverse())), 8) self.assertEqual(len(list(t.traverse(self_before=True))), 15) #check that leaves were created in right order self.assertEqual([i.Id for i in t.traverse()], range(8)) #check that whole toplogy is right wrt ids... nodes = list(t.traverse(self_before=True)) obs = [i.Id for i in nodes] exp = [8, 9, 11, 13, 0, 1, 2, 12, 3, 4, 10, 14, 5, 6, 7] self.assertEqual(obs, exp) #...and ranges obs = [i.LeafRange for i in nodes] exp = [(0,8),(0,5),(0,3),(0,2),(0,1),(1,2),(2,3),(3,5),(3,4),(4,5), \ (5,8),(5,7),(5,6),(6,7),(7,8)] self.assertEqual(obs, exp)
def make_tree_arb_safe(t): """Gives a second child to all single descendent nodes""" for n in t.nontips(include_self=True): if len(n.Children) == 1: n.append(RangeNode(Name="X",Length=0.0))