def do_distance_analysis(X): # get the matrix of squared distances labels = list('0123') # reconstruct the matrix of Euclidean distances from a tree D_sqrt = np.array([[np.linalg.norm(y - x) for x in X] for y in X]) sqrt_tree = NeighborJoining.make_tree(D_sqrt, labels) sqrt_tree_string = NewickIO.get_newick_string(sqrt_tree) sqrt_feltree = NewickIO.parse(sqrt_tree_string, FelTree.NewickTree) D_sqrt_reconstructed = np.array(sqrt_feltree.get_distance_matrix(labels)) # reconstruct the matrix of squared Euclidean distances from a tree D = D_sqrt**2 tree = NeighborJoining.make_tree(D, labels) tree_string = NewickIO.get_newick_string(tree) feltree = NewickIO.parse(tree_string, FelTree.NewickTree) D_reconstructed = np.array(feltree.get_distance_matrix(labels)) # start writing out = StringIO() # matrix of Euclidean distances and its reconstruction from a tree print >> out, 'matrix of Euclidean distances between tetrahedron vertices:' print >> out, D_sqrt print >> out, 'neighbor joining tree constructed from D = non-squared Euclidean distances (unusual):' print >> out, sqrt_tree_string print >> out, 'distance matrix implied by this tree:' print >> out, D_sqrt_reconstructed # matrix of squared Euclidean distances and its reconstruction from a tree print >> out, 'matrix of squared distances between tetrahedron vertices:' print >> out, D print >> out, 'neighbor joining tree constructed from D = squared Euclidean distances (normal):' print >> out, tree_string print >> out, 'distance matrix implied by this tree:' print >> out, D_reconstructed return out.getvalue().strip()
def do_distance_analysis(X): # get the matrix of squared distances labels = list("0123") # reconstruct the matrix of Euclidean distances from a tree D_sqrt = np.array([[np.linalg.norm(y - x) for x in X] for y in X]) sqrt_tree = NeighborJoining.make_tree(D_sqrt, labels) sqrt_tree_string = NewickIO.get_newick_string(sqrt_tree) sqrt_feltree = NewickIO.parse(sqrt_tree_string, FelTree.NewickTree) D_sqrt_reconstructed = np.array(sqrt_feltree.get_distance_matrix(labels)) # reconstruct the matrix of squared Euclidean distances from a tree D = D_sqrt ** 2 tree = NeighborJoining.make_tree(D, labels) tree_string = NewickIO.get_newick_string(tree) feltree = NewickIO.parse(tree_string, FelTree.NewickTree) D_reconstructed = np.array(feltree.get_distance_matrix(labels)) # start writing out = StringIO() # matrix of Euclidean distances and its reconstruction from a tree print >> out, "matrix of Euclidean distances between tetrahedron vertices:" print >> out, D_sqrt print >> out, "neighbor joining tree constructed from D = non-squared Euclidean distances (unusual):" print >> out, sqrt_tree_string print >> out, "distance matrix implied by this tree:" print >> out, D_sqrt_reconstructed # matrix of squared Euclidean distances and its reconstruction from a tree print >> out, "matrix of squared distances between tetrahedron vertices:" print >> out, D print >> out, "neighbor joining tree constructed from D = squared Euclidean distances (normal):" print >> out, tree_string print >> out, "distance matrix implied by this tree:" print >> out, D_reconstructed return out.getvalue().strip()
def get_response_content(fs): # read the matrix D = fs.matrix if len(D) < 3: raise HandlingError('the matrix should have at least three rows') # read the ordered labels ordered_labels = Util.get_stripped_lines(fs.labels.splitlines()) if len(ordered_labels) != len(D): msg_a = 'the number of ordered labels should be the same ' msg_b = 'as the number of rows in the matrix' raise HandlingError(msg_a + msg_b) # get the newick tree tree = NeighborJoining.make_tree(D.tolist(), ordered_labels) # return the response return NewickIO.get_newick_string(tree) + '\n'
def get_response_content(fs): # read the matrix D = fs.matrix if len(D) < 3: raise HandlingError('the matrix should have at least three rows') # read the ordered labels ordered_labels = Util.get_stripped_lines(fs.labels.splitlines()) if len(ordered_labels) != len(D): msg_a = 'the number of ordered labels should be the same ' msg_b = 'as the number of rows in the matrix' raise HandlingError(msg_a + msg_b) # get the newick tree tree = NeighborJoining.make_tree(D.tolist(), ordered_labels) # return the response return NewickIO.get_newick_string(tree) + '\n'