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_bind_to_parent_array(self): """bind_to_parent_array should bind tree to array correctly""" a = reshape(arange(33), (11,3)) index_tree(self.t3) bindings = bind_to_parent_array(self.t3, a) self.assertEqual(len(bindings), 10) self.assertEqual(bindings[0][0], a[8]) self.assertEqual(bindings[0][1], a[10]) self.assertEqual(bindings[1][0], a[4]) self.assertEqual(bindings[1][1], a[8]) self.assertEqual(bindings[2][0], a[0]) self.assertEqual(bindings[2][1], a[4]) self.assertEqual(bindings[3][0], a[1]) self.assertEqual(bindings[3][1], a[4]) self.assertEqual(bindings[4][0], a[2]) self.assertEqual(bindings[4][1], a[4]) self.assertEqual(bindings[5][0], a[5]) self.assertEqual(bindings[5][1], a[8]) self.assertEqual(bindings[6][0], a[3]) self.assertEqual(bindings[6][1], a[5]) self.assertEqual(bindings[7][0], a[9]) self.assertEqual(bindings[7][1], a[10]) self.assertEqual(bindings[8][0], a[6]) self.assertEqual(bindings[8][1], a[9]) self.assertEqual(bindings[9][0], a[7]) self.assertEqual(bindings[9][1], a[9])
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_bind_to_parent_array(self): """bind_to_parent_array should bind tree to array correctly""" a = reshape(arange(33), (11, 3)) index_tree(self.t3) bindings = bind_to_parent_array(self.t3, a) self.assertEqual(len(bindings), 10) self.assertEqual(bindings[0][0], a[8]) self.assertEqual(bindings[0][1], a[10]) self.assertEqual(bindings[1][0], a[4]) self.assertEqual(bindings[1][1], a[8]) self.assertEqual(bindings[2][0], a[0]) self.assertEqual(bindings[2][1], a[4]) self.assertEqual(bindings[3][0], a[1]) self.assertEqual(bindings[3][1], a[4]) self.assertEqual(bindings[4][0], a[2]) self.assertEqual(bindings[4][1], a[4]) self.assertEqual(bindings[5][0], a[5]) self.assertEqual(bindings[5][1], a[8]) self.assertEqual(bindings[6][0], a[3]) self.assertEqual(bindings[6][1], a[5]) self.assertEqual(bindings[7][0], a[9]) self.assertEqual(bindings[7][1], a[10]) self.assertEqual(bindings[8][0], a[6]) self.assertEqual(bindings[8][1], a[9]) self.assertEqual(bindings[9][0], a[7]) self.assertEqual(bindings[9][1], a[9])
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_tip_distances(self): """tip_distances should set tips to correct distances.""" t = self.t bl = self.branch_lengths.copy()[:,newaxis] bindings = bind_to_parent_array(t, bl) tips = [] for n in t.traverse(self_before=False, self_after=True): if not n.Children: tips.append(n._leaf_index) tip_distances(bl, bindings, tips) self.assertEqual(bl, array([5,6,6,6,6,0,0,0,0])[:,newaxis])
def test_tip_distances(self): """tip_distances should set tips to correct distances.""" t = self.t bl = self.branch_lengths.copy()[:, newaxis] bindings = bind_to_parent_array(t, bl) tips = [] for n in t.traverse(self_before=False, self_after=True): if not n.Children: tips.append(n._leaf_index) tip_distances(bl, bindings, tips) self.assertEqual(bl, array([5, 6, 6, 6, 6, 0, 0, 0, 0])[:, newaxis])