def test_sum_descendants(self): """sum_descendants should sum total descendants w/ each state""" 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] sum_descendants(bindings) self.assertEqual( a, array( [ [0, 1, 0], [0, 1, 0], [0, 1, 0], [1, 0, 0], [0, 3, 0], [1, 0, 0], [0, 0, 1], [0, 1, 0], [1, 3, 0], [0, 1, 1], [1, 4, 1], ] ), )
def test_weighted_unifrac_matrix(self): """weighted unifrac matrix should ret correct results for model tree""" # should match web site calculations envs = self.count_array bound_indices = bind_to_array(self.nodes, envs) sum_descendants(bound_indices) bl = self.branch_lengths tip_indices = [n._leaf_index for n in self.t.tips()] result = weighted_unifrac_matrix(bl, envs, tip_indices) exp = array([[0, 9.1, 4.5], [9.1, 0, 6.4], [4.5, 6.4, 0]]) assert (abs(result - exp)).max() < 0.001 # should work with branch length corrections td = bl.copy()[:, newaxis] tip_bindings = bind_to_parent_array(self.t, td) tips = [n._leaf_index for n in self.t.tips()] tip_distances(td, tip_bindings, tips) result = weighted_unifrac_matrix(bl, envs, tip_indices, bl_correct=True, tip_distances=td) exp = array( [ [0, 9.1 / 11.5, 4.5 / (10.5 + 1.0 / 3)], [9.1 / 11.5, 0, 6.4 / (11 + 1.0 / 3)], [4.5 / (10.5 + 1.0 / 3), 6.4 / (11 + 1.0 / 3), 0], ] ) assert (abs(result - exp)).max() < 0.001
def test_weighted_unifrac_matrix(self): """weighted unifrac matrix should return correct results for model tree""" #should match web site calculations envs = self.count_array bound_indices = bind_to_array(self.nodes, envs) sum_descendants(bound_indices) bl = self.branch_lengths tip_indices = [n._leaf_index for n in self.t.tips()] result = weighted_unifrac_matrix(bl, envs, tip_indices) exp = array([[0, 9.1, 4.5], [9.1, 0, \ 6.4], [4.5, 6.4, 0]]) assert (abs(result - exp)).max() < 0.001 #should work with branch length corrections td = bl.copy()[:, newaxis] tip_bindings = bind_to_parent_array(self.t, td) tips = [n._leaf_index for n in self.t.tips()] tip_distances(td, tip_bindings, tips) result = weighted_unifrac_matrix(bl, envs, tip_indices, bl_correct=True, tip_distances=td) exp = array([[0, 9.1/11.5, 4.5/(10.5+1./3)], [9.1/11.5, 0, \ 6.4/(11+1./3)], [4.5/(10.5+1./3), 6.4/(11+1./3), 0]]) assert (abs(result - exp)).max() < 0.001
def test_weighted_one_sample(self): """weighted one sample should match weighted matrix""" #should match web site calculations envs = self.count_array bound_indices = bind_to_array(self.nodes, envs) sum_descendants(bound_indices) bl = self.branch_lengths tip_indices = [n._leaf_index for n in self.t.tips()] result = weighted_unifrac_matrix(bl, envs, tip_indices) for i in range(len(result)): one_sam_res = weighted_one_sample(i, bl, envs, tip_indices) self.assertEqual(result[i], one_sam_res) self.assertEqual(result[:,i], one_sam_res) #should work with branch length corrections td = bl.copy()[:,newaxis] tip_bindings = bind_to_parent_array(self.t, td) tips = [n._leaf_index for n in self.t.tips()] tip_distances(td, tip_bindings, tips) result = weighted_unifrac_matrix(bl, envs, tip_indices, bl_correct=True, tip_distances=td) for i in range(len(result)): one_sam_res = weighted_one_sample(i, bl, envs, tip_indices, bl_correct=True, tip_distances=td) self.assertEqual(result[i], one_sam_res) self.assertEqual(result[:,i], one_sam_res)
def test_weighted_one_sample(self): """weighted one sample should match weighted matrix""" #should match web site calculations envs = self.count_array bound_indices = bind_to_array(self.nodes, envs) sum_descendants(bound_indices) bl = self.branch_lengths tip_indices = [n._leaf_index for n in self.t.tips()] result = weighted_unifrac_matrix(bl, envs, tip_indices) for i in range(len(result)): one_sam_res = weighted_one_sample(i, bl, envs, tip_indices) self.assertEqual(result[i], one_sam_res) self.assertEqual(result[:, i], one_sam_res) #should work with branch length corrections td = bl.copy()[:, newaxis] tip_bindings = bind_to_parent_array(self.t, td) tips = [n._leaf_index for n in self.t.tips()] tip_distances(td, tip_bindings, tips) result = weighted_unifrac_matrix(bl, envs, tip_indices, bl_correct=True, tip_distances=td) for i in range(len(result)): one_sam_res = weighted_one_sample(i, bl, envs, tip_indices, bl_correct=True, tip_distances=td) self.assertEqual(result[i], one_sam_res) self.assertEqual(result[:, i], one_sam_res)
def test_weighted_unifrac_vector(self): """weighted_unifrac_vector should ret correct results for model tree""" envs = self.count_array bound_indices = bind_to_array(self.nodes, envs) sum_descendants(bound_indices) bl = self.branch_lengths tip_indices = [n._leaf_index for n in self.t.tips()] result = weighted_unifrac_vector(bl, envs, tip_indices) self.assertFloatEqual( result[0], sum( [ abs(1.0 / 2 - 2.0 / 8) * 1, abs(1.0 / 2 - 1.0 / 8) * 2, abs(0 - 1.0 / 8) * 3, abs(0 - 3.0 / 8) * 1, abs(0 - 1.0 / 8) * 1, abs(0 - 4.0 / 8) * 2, abs(2.0 / 2 - 3.0 / 8) * 4, abs(0.0 - 5.0 / 8) * 3.0, ] ), ) self.assertFloatEqual( result[1], sum( [ abs(0 - 0.6) * 1, abs(0.2 - 0.2) * 2, abs(0.2 - 0) * 3, abs(0.6 - 0) * 1, abs(0 - 0.2) * 1, abs(0.6 - 0.2) * 2, abs(0.2 - 0.8) * 4, abs(0.8 - 0.2) * 3, ] ), ) self.assertFloatEqual( result[2], sum( [ abs(2.0 / 3 - 1.0 / 7) * 1, abs(0 - 2.0 / 7) * 2, abs(0 - 1.0 / 7) * 3, abs(0 - 3.0 / 7) * 1, abs(1.0 / 3 - 0) * 1, abs(1.0 / 3 - 3.0 / 7) * 2, abs(2.0 / 3 - 3.0 / 7) * 4, abs(1.0 / 3 - 4.0 / 7) * 3, ] ), )
def test_sum_descendants(self): """sum_descendants should sum total descendants w/ each state""" 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] sum_descendants(bindings) self.assertEqual(a, \ array([[0,1,0],[0,1,0],[0,1,0],[1,0,0],[0,3,0],[1,0,0],\ [0,0,1],[0,1,0],[1,3,0],[0,1,1],[1,4,1]]) )
def test_weighted_unifrac_vector(self): """weighted_unifrac_vector should return correct results for model tree""" envs = self.count_array bound_indices = bind_to_array(self.nodes, envs) sum_descendants(bound_indices) bl = self.branch_lengths tip_indices = [n._leaf_index for n in self.t.tips()] result = weighted_unifrac_vector(bl, envs, tip_indices) self.assertFloatEqual( result[0], sum([ abs(1. / 2 - 2. / 8) * 1, abs(1. / 2 - 1. / 8) * 2, abs(0 - 1. / 8) * 3, abs(0 - 3. / 8) * 1, abs(0 - 1. / 8) * 1, abs(0 - 4. / 8) * 2, abs(2. / 2 - 3. / 8) * 4, abs(0. - 5. / 8) * 3. ])) self.assertFloatEqual( result[1], sum([ abs(0 - .6) * 1, abs(.2 - .2) * 2, abs(.2 - 0) * 3, abs(.6 - 0) * 1, abs(0 - .2) * 1, abs(.6 - .2) * 2, abs(.2 - .8) * 4, abs(.8 - .2) * 3 ])) self.assertFloatEqual( result[2], sum([ abs(2. / 3 - 1. / 7) * 1, abs(0 - 2. / 7) * 2, abs(0 - 1. / 7) * 3, abs(0 - 3. / 7) * 1, abs(1. / 3 - 0) * 1, abs(1. / 3 - 3. / 7) * 2, abs(2. / 3 - 3. / 7) * 4, abs(1. / 3 - 4. / 7) * 3 ]))