def main(): trees = dendropy.TreeList.get_from_path(sys.argv[1], sys.argv[2]) for tree in trees: pdm = treecalc.PatristicDistanceMatrix(tree) tree.wt_profile = sorted(pdm.distances()) tree.uwt_profile = [] for txidx1 in range(len(tree.taxon_set) - 1): for txidx2 in range(txidx1 + 1, len(tree.taxon_set)): tree.uwt_profile.append( pdm.path_edge_count(tree.taxon_set[txidx1], tree.taxon_set[txidx2])) tree.uwt_profile = sorted(tree.uwt_profile) tree_length = tree.length() if tree_length > 0: tree.swt_profile = [ float(x) / tree_length for x in tree.wt_profile ] else: tree.swt_profile = [0.0 for x in tree.wt_profile] for tidx1 in range(len(trees)): for tidx2 in range(len(trees)): sys.stdout.write("{}\t{}\t{:.22f}\t{:.22f}\t{:.22f}\n".format( tidx1, tidx2, euclidean_distance(trees[tidx1].uwt_profile, trees[tidx2].uwt_profile), euclidean_distance(trees[tidx1].wt_profile, trees[tidx2].wt_profile), euclidean_distance(trees[tidx1].swt_profile, trees[tidx2].swt_profile)))
def to_distance_matrix(tree): """Create a distance matrix (NumPy array) from clades/branches in tree. Returns a tuple of (allclades, distance_matrix) where allclades is a list of clades and distance_matrix is a 2D array. """ pdm = treecalc.PatristicDistanceMatrix(tree) matrixStrainList = tree.taxon_set return (matrixStrainList, pdm)
def branchOut(self, centerTaxon, subsetSize, **kwargs): dist = {} pdm = treecalc.PatristicDistanceMatrix(self.den_tree) for i, s in enumerate(self.den_tree.taxon_set): # @UnusedVariable if "filterTaxon" in kwargs: if not kwargs["filterTaxon"](s): continue dist[s.label] = pdm(centerTaxon, s) incircle = sortByValue(dist)[0:subsetSize] return [node[0] for node in incircle]
def calculate_pairwise_tree_dists(intree, output): tree = dendropy.Tree.get_from_path(intree, "newick", preserve_underscores=True) outfile = open("%s" % output, "w") distances = treecalc.PatristicDistanceMatrix(tree) for i, t1 in enumerate(tree.taxon_set): for t2 in tree.taxon_set[i + 1:]: print >> outfile, "Distance between '%s' and '%s': %s" % ( t1.label, t2.label, distances(t1, t2)) outfile.close()
def testPatDistMatrix(self): pdm = treecalc.PatristicDistanceMatrix(self.tree) def _chk_distance(pdm, t1, t2, exp_distance): tax1 = self.tree.taxon_set.require_taxon(label=t1) tax2 = self.tree.taxon_set.require_taxon(label=t2) pd = pdm(tax1, tax2) self.assertEqual(pd, exp_distance) _chk_distance(pdm, "a", "b", 2) _chk_distance(pdm, "a", "c", 4) _chk_distance(pdm, "b", "c", 4) _chk_distance(pdm, "a", "d", 6) _chk_distance(pdm, "f", "d", 4) _chk_distance(pdm, "c", "d", 6)
#! /usr/bin/env python # Calculates a patristic distance matrix between all leaves in a tree. # The name of the tree file (nexus format) should be provided as the first argument. # The name of the matrix output file should be provided as the second argument. # Uses Dendropy 3 import dendropy from dendropy import treecalc import sys tree = dendropy.Tree.get_from_path(sys.argv[1], "nexus", preserve_underscores=True) pdm = treecalc.PatristicDistanceMatrix(tree) matFile = open(sys.argv[2], 'w') matFile.write("\t") for t in tree.taxon_set: matFile.write("%s\t" % t.label, ) matFile.write('\n') for i, t1 in enumerate(tree.taxon_set): matFile.write("%s\t" % t1.label, ) matFile.write('\t' * (i + 1), ) for t2 in tree.taxon_set[i + 1:]: matFile.write("%s\t" % pdm(t1, t2)) matFile.write('\n')