def condense_node_order(matrix, smallest_index, node_order): """condenses two nodes in node_order based on smallest_index info This function is used to create a tree while condensing a matrix with the condense_matrix function. The smallest_index is retrieved with find_smallest_index. The first index is replaced with a node object that combines the two nodes corresponding to the indices in node order. The second index in smallest_index is replaced with None. Also sets the branch length of the nodes to 1/2 of the distance between the nodes in the matrix""" index1 = smallest_index[0] index2 = smallest_index[1] node1 = node_order[index1] node2 = node_order[index2] #get the distance between the nodes and assign 1/2 the distance to the #BranchLength property of each node distance = matrix[index1, index2] nodes = [node1,node2] d = distance/2.0 for n in nodes: if n.Children: n.BranchLength = d - n.Children[0].TipLength else: n.BranchLength = d n.TipLength = d #combine the two nodes into a new PhyloNode object new_node = PhyloNode() new_node.append(node1) new_node.append(node2) #replace the object at index1 with the combined node node_order[index1] = new_node #replace the object at index2 with None node_order[index2] = None return node_order
def setUp(self): """creates inputs""" #create a list of PhyloNode objects a = PhyloNode() a.Data = 'a' b = PhyloNode() b.Data = 'b' c = PhyloNode() c.Data = 'c' d = PhyloNode() d.Data = 'd' e = PhyloNode() e.Data = 'e' self.node_order = [a, b, c, d, e] #create a Numeric matrix object to cluster self.matrix = array(([9999999, 1, 4, 20, 22], \ [1, 9999999, 5, 21, 23], \ [4, 5, 9999999, 10, 12], \ [20, 21, 10, 9999999, 2], \ [22, 23, 12, 2, 9999999]), Float)
def test_ops(self): """Basic PhyloNode operations should work as expected""" p = PhyloNode() self.assertEqual(str(p), "()") p.Data = "abc" self.assertEqual(str(p), "()abc") p.BranchLength = 3 self.assertEqual(str(p), "()abc") # suppress branch from root q = PhyloNode() p.append(q) self.assertEqual(str(p), "(None)abc") r = PhyloNode() q.append(r) self.assertEqual(str(p), "((None))abc") r.Data = "xyz" self.assertEqual(str(p), "((xyz))abc") q.BranchLength = 2 self.assertEqual(str(p), "((xyz):2)abc")