def topologyDistanceMatrix(self, metric='sd'): """Returns a DistanceMatrix object showing topology distances. Uses the :meth:`Tree.Tree.topologyDistance` method to compare trees. That method returns distances between pairs of trees, and this method simply collates those distances into a :class:`DistanceMatrix.DistanceMatrix` object, which is returned. See :meth:`Tree.Tree.topologyDistance` for an explanation of the different metrics. The metrics are given in ``var.topologyDistanceMetrics``""" from distancematrix import DistanceMatrix d = DistanceMatrix() d.names = [] for t in self.trees: d.names.append(t.name) d.dim = len(d.names) # print d.names # sys.exit() d.matrix = [] for i in range(d.dim): d.matrix.append([0.0] * d.dim) for i in range(d.dim): t1 = self.trees[i] for j in range(i + 1, d.dim): t2 = self.trees[j] theDist = t1.topologyDistance(t2, metric=metric) d.matrix[i][j] = theDist d.matrix[j][i] = theDist return d
def patristicDistanceMatrix(self): """Matrix of distances along tree path. This method sums the branch lengths between each pair of taxa, and puts the result in a DistanceMatrix object, which is returned. Self.taxNames is required. """ gm = ['Tree.patristicDistanceMatrix()'] if not self.taxNames: gm.append("No taxNames.") raise P4Error(gm) # The tree will be rearranged, so make a copy to play with, so # self is undisturbed. t = self.dupe() d = DistanceMatrix() d.names = self.taxNames d.dim = len(d.names) # print d.names d.matrix = [] for i in range(d.dim): d.matrix.append([0.0] * d.dim) for i in range(d.dim): n1 = t.node(d.names[i]) t.reRoot(n1, moveInternalName=False) for j in range(i + 1, d.dim): n2 = t.node(d.names[j]) # sum up distances between n1 and n2 sum = n2.br.len n2 = n2.parent while n2 != n1: sum = sum + n2.br.len n2 = n2.parent # print "Dist from %s to %s is %f" % (d.names[i], d.names[j], # sum) d.matrix[i][j] = sum d.matrix[j][i] = sum return d