def test_format_phylip(self): dm = DistanceMatrix(self.names, self.matrix) handle = StringIO() dm.format_phylip(handle) lines = handle.getvalue().splitlines() self.assertEqual(len(lines), len(dm) + 1) self.assertTrue(lines[0].endswith(str(len(dm)))) for name, line in zip(self.names, lines[1:]): self.assertTrue(line.startswith(name))
def test_format_phylip(self): dm = DistanceMatrix(self.names, self.matrix) handle = StringIO() dm.format_phylip(handle) lines = handle.getvalue().splitlines() self.assertEqual(len(lines), len(dm) + 1) self.assertTrue(lines[0].endswith(str(len(dm)))) for name, line in zip(self.names, lines[1:]): self.assertTrue(line.startswith(name))
def dendrogram_biopython(condensed_distance_matrix_jaccard, organisms): """ Create a lower triangle matrix. Then create a biopython dendrogram. Parameters ---------- condensed_distance_matrix_jaccard: ndarray Condensed Jaccard distance matrix organisms: list organisms names """ from Bio.Phylo.TreeConstruction import DistanceTreeConstructor, DistanceMatrix from Bio.Phylo import draw_ascii lower_triangle_matrix = [ list(v[:i + 1]) for i, v in enumerate(squareform(condensed_distance_matrix_jaccard)) ] constructor = DistanceTreeConstructor() dm = DistanceMatrix(organisms, lower_triangle_matrix) tree = constructor.nj(dm) draw_ascii(tree) dm.format_phylip(open('test.phy', 'w'))
s1_prob += t_mat[j][base_num[s1[k-1]]][base_num[s1[k]]] s2_prob=0 for k in range(1, len(s2)): s2_prob += t_mat[i][base_num[s2[k-1]]][base_num[s2[k]]] distance1 = s1_prob+s2_prob ''' distance2 = np.linalg.norm(t_mat[i] - t_mat[j], ord=2) #temp1.append(distance1) temp2.append(distance2) #d_mat1.append(temp1) d_mat2.append(temp2) #distance_matrix1 = DistanceMatrix(names, d_mat1) #distance_matrix1.format_phylip(open(path+str(replicate)+"/d_matMM.phy", 'wt')) distance_matrix2 = DistanceMatrix(names, d_mat2) distance_matrix2.format_phylip( open(path + str(replicate) + "/d_matl2.phy", 'wt')) ''' calculator = DistanceCalculator('ident') constructor_nj = DistanceTreeConstructor(calculator, 'nj') tree_nj = constructor_nj.build_tree(aln) print(tree_nj) Phylo.write(tree_nj, 'out_tre', 'newick') '''