def test_bool_descendants(self): """bool_descendants should be true if any descendant true""" # self.t3 = DndParser('(((a,b,c),(d)),(e,f))', UniFracTreeNode) id_, child = index_tree(self.t3) a = zeros((11, 3)) + 99 # fill with junk bindings = bind_to_array(child, a) # load in leaf envs a[0] = a[1] = a[2] = a[7] = [0, 1, 0] a[3] = [1, 0, 0] a[6] = [0, 0, 1] bool_descendants(bindings) self.assertEqual( a, array( [ [0, 1, 0], [0, 1, 0], [0, 1, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [0, 0, 1], [0, 1, 0], [1, 1, 0], [0, 1, 1], [1, 1, 1], ] ), )
def test_unifrac_matrix(self): """unifrac_matrix should return correct results for model tree""" m = array([[1,0,1],[1,1,0],[0,1,0],[0,0,1],[0,1,0],[0,1,1],[1,1,1],\ [0,1,1],[1,1,1]]) bl = self.branch_lengths result = unifrac_matrix(bl, m) self.assertEqual(result, array([[0, 10/16.,8/13.],[10/16.,0,8/17.],\ [8/13.,8/17.,0]])) #should work if we tell it the measure is asymmetric result = unifrac_matrix(bl, m, is_symmetric=False) self.assertEqual(result, array([[0, 10/16.,8/13.],[10/16.,0,8/17.],\ [8/13.,8/17.,0]])) #should work if the measure really is asymmetric result = unifrac_matrix(bl, m, metric=unnormalized_G, is_symmetric=False) self.assertEqual(result, array([[0, 1/17.,2/17.],[9/17.,0,6/17.],\ [6/17.,2/17.,0]])) #should also match web site calculations envs = self.count_array bound_indices = bind_to_array(self.nodes, envs) bool_descendants(bound_indices) result = unifrac_matrix(bl, envs) exp = array([[0, 0.6250, 0.6154], [0.6250, 0, \ 0.4706], [0.6154, 0.4707, 0]]) assert (abs(result - exp)).max() < 0.001
def test_env_unique_fraction(self): """should report unique fraction of bl in each env """ # testing old unique fraction cur_count_array = self.count_array.copy() bound_indices = bind_to_array(self.nodes, cur_count_array) total_bl = sum(self.branch_lengths) bool_descendants(bound_indices) env_bl_sums, env_bl_ufracs = env_unique_fraction( self.branch_lengths, cur_count_array) # env A has 0 unique bl, B has 4, C has 1 self.assertEqual(env_bl_sums, [0, 4, 1]) self.assertEqual(env_bl_ufracs, [0, 4 / 17.0, 1 / 17.0]) cur_count_array = self.old_count_array.copy() bound_indices = bind_to_array(self.old_nodes, cur_count_array) total_bl = sum(self.old_branch_lengths) bool_descendants(bound_indices) env_bl_sums, env_bl_ufracs = env_unique_fraction( self.old_branch_lengths, cur_count_array) # env A has 0 unique bl, B has 4, C has 1 self.assertEqual(env_bl_sums, env_bl_sums) self.assertEqual(env_bl_sums, [1.29, 0.33999999999999997, 0.63]) self.assertEqual(env_bl_ufracs, [1.29 / 2.9, 0.33999999999999997 / 2.9, 0.63 / 2.9])
def test_delete_empty_parents(self): """delete_empty_parents should remove empty parents from bound indices""" id_to_node, node_first_last = index_tree(self.t) bound_indices = bind_to_array(node_first_last, self.count_array[:,0:1]) bool_descendants(bound_indices) self.assertEqual(len(bound_indices), 4) deleted = delete_empty_parents(bound_indices) self.assertEqual(len(deleted), 2) for d in deleted: self.assertEqual(d[0][0], 1)
def test_delete_empty_parents(self): """delete_empty_parents should remove empty parents from bound indices""" id_to_node, node_first_last = index_tree(self.t) bound_indices = bind_to_array(node_first_last, self.count_array[:, 0:1]) bool_descendants(bound_indices) self.assertEqual(len(bound_indices), 4) deleted = delete_empty_parents(bound_indices) self.assertEqual(len(deleted), 2) for d in deleted: self.assertEqual(d[0][0], 1)
def test_bool_descendants(self): """bool_descendants should be true if any descendant true""" #self.t3 = DndParser('(((a,b,c),(d)),(e,f))', UniFracTreeNode) id_, child = index_tree(self.t3) a = zeros((11, 3)) + 99 #fill with junk bindings = bind_to_array(child, a) #load in leaf envs a[0] = a[1] = a[2] = a[7] = [0, 1, 0] a[3] = [1, 0, 0] a[6] = [0, 0, 1] bool_descendants(bindings) self.assertEqual(a, \ array([[0,1,0],[0,1,0],[0,1,0],[1,0,0],[0,1,0],[1,0,0],\ [0,0,1],[0,1,0],[1,1,0],[0,1,1],[1,1,1]]) )
def test_unifrac_matrix(self): """unifrac_matrix should return correct results for model tree""" m = array([[1, 0, 1], [1, 1, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1]]) bl = self.branch_lengths result = unifrac_matrix(bl, m) self.assertEqual(result, array([[0, 10 / 16.0, 8 / 13.0], [10 / 16.0, 0, 8 / 17.0], [8 / 13.0, 8 / 17.0, 0]])) # should work if we tell it the measure is asymmetric result = unifrac_matrix(bl, m, is_symmetric=False) self.assertEqual(result, array([[0, 10 / 16.0, 8 / 13.0], [10 / 16.0, 0, 8 / 17.0], [8 / 13.0, 8 / 17.0, 0]])) # should work if the measure really is asymmetric result = unifrac_matrix(bl, m, metric=unnormalized_G, is_symmetric=False) self.assertEqual(result, array([[0, 1 / 17.0, 2 / 17.0], [9 / 17.0, 0, 6 / 17.0], [6 / 17.0, 2 / 17.0, 0]])) # should also match web site calculations envs = self.count_array bound_indices = bind_to_array(self.nodes, envs) bool_descendants(bound_indices) result = unifrac_matrix(bl, envs) exp = array([[0, 0.6250, 0.6154], [0.6250, 0, 0.4706], [0.6154, 0.4707, 0]]) assert (abs(result - exp)).max() < 0.001
def test_env_unique_fraction(self): """should report unique fraction of bl in each env """ # testing old unique fraction cur_count_array = self.count_array.copy() bound_indices = bind_to_array(self.nodes, cur_count_array) total_bl = sum(self.branch_lengths) bool_descendants(bound_indices) env_bl_sums, env_bl_ufracs = env_unique_fraction(self.branch_lengths, cur_count_array) # env A has 0 unique bl, B has 4, C has 1 self.assertEqual(env_bl_sums, [0,4,1]) self.assertEqual(env_bl_ufracs, [0,4/17.0,1/17.0]) cur_count_array = self.old_count_array.copy() bound_indices = bind_to_array(self.old_nodes, cur_count_array) total_bl = sum(self.old_branch_lengths) bool_descendants(bound_indices) env_bl_sums, env_bl_ufracs = env_unique_fraction(self.old_branch_lengths, cur_count_array) # env A has 0 unique bl, B has 4, C has 1 self.assertEqual(env_bl_sums, env_bl_sums) self.assertEqual(env_bl_sums, [1.29, 0.33999999999999997, 0.63]) self.assertEqual(env_bl_ufracs, [1.29/2.9,0.33999999999999997/2.9, 0.63/2.9])