def test_mito_matrix(self): D = g_mito_matrix n = len(D) observed_Q = get_Q_matrix(D) expected_Q = g_mito_matrix_q # assert that the diagonal elements of the observed Q matrix are exactly zero for i, row in enumerate(observed_Q): self.assertEqual(row[i], 0) # assert that the observed Q matrix is approximately equal to the expected Q matrix abs_tol = .001 for i in range(n): for j in range(n): abs_delta = abs(observed_Q[i][j] - expected_Q[i][j]) self.failUnless(abs_delta < abs_tol) # use neighbor joining to reconstruct the tree observed_tree = make_tree(D, g_mito_states) # load the expected tree expected_tree = NewickIO.parse(g_mito_tree_string, FelTree.NewickTree) # for the observed and expected trees calculate the induced partitions and corresponding branch lengths observed_partitions_and_lengths = TreeComparison.get_partitions_and_branch_lengths( observed_tree) expected_partitions_and_lengths = TreeComparison.get_partitions_and_branch_lengths( expected_tree) # the number of partitions should be the same self.assertEqual(len(observed_partitions_and_lengths), len(expected_partitions_and_lengths)) # the partitions should be the same observed_partitions = set( [part for part, length in observed_partitions_and_lengths]) expected_partitions = set( [part for part, length in expected_partitions_and_lengths]) observed_only = observed_partitions - expected_partitions expected_only = expected_partitions - observed_partitions lines = [ 'observed partitions include: ' + str(observed_only), 'expected partitions include: ' + str(expected_only) ] self.assertEqual(observed_partitions, expected_partitions, '\n'.join(lines)) # corresponding partitions should have the same lengths observed_part_to_length = dict(observed_partitions_and_lengths) expected_part_to_length = dict(expected_partitions_and_lengths) lines = [] for part in observed_partitions: observed_length = observed_part_to_length[part] expected_length = expected_part_to_length[part] abs_tol = .00001 abs_delta = abs(observed_length - expected_length) if abs_delta > abs_tol: lines.append('partition:' + str(part)) lines.append('observed branch length:' + str(observed_length)) lines.append('expected branch length:' + str(expected_length)) error_message = '\n'.join(lines) self.failIf(error_message, error_message)
def test_mito_matrix(self): D = g_mito_matrix n = len(D) observed_Q = get_Q_matrix(D) expected_Q = g_mito_matrix_q # assert that the diagonal elements of the observed Q matrix are exactly zero for i, row in enumerate(observed_Q): self.assertEqual(row[i], 0) # assert that the observed Q matrix is approximately equal to the expected Q matrix abs_tol = .001 for i in range(n): for j in range(n): abs_delta = abs(observed_Q[i][j] - expected_Q[i][j]) self.failUnless(abs_delta < abs_tol) # use neighbor joining to reconstruct the tree observed_tree = make_tree(D, g_mito_states) # load the expected tree expected_tree = NewickIO.parse(g_mito_tree_string, FelTree.NewickTree) # for the observed and expected trees calculate the induced partitions and corresponding branch lengths observed_partitions_and_lengths = TreeComparison.get_partitions_and_branch_lengths(observed_tree) expected_partitions_and_lengths = TreeComparison.get_partitions_and_branch_lengths(expected_tree) # the number of partitions should be the same self.assertEqual(len(observed_partitions_and_lengths), len(expected_partitions_and_lengths)) # the partitions should be the same observed_partitions = set([part for part, length in observed_partitions_and_lengths]) expected_partitions = set([part for part, length in expected_partitions_and_lengths]) observed_only = observed_partitions - expected_partitions expected_only = expected_partitions - observed_partitions lines = [ 'observed partitions include: ' + str(observed_only), 'expected partitions include: ' + str(expected_only) ] self.assertEqual(observed_partitions, expected_partitions, '\n'.join(lines)) # corresponding partitions should have the same lengths observed_part_to_length = dict(observed_partitions_and_lengths) expected_part_to_length = dict(expected_partitions_and_lengths) lines = [] for part in observed_partitions: observed_length = observed_part_to_length[part] expected_length = expected_part_to_length[part] abs_tol = .00001 abs_delta = abs(observed_length - expected_length) if abs_delta > abs_tol: lines.append('partition:' + str(part)) lines.append('observed branch length:' + str(observed_length)) lines.append('expected branch length:' + str(expected_length)) error_message = '\n'.join(lines) self.failIf(error_message, error_message)