def main(tree_fn): if not os.path.exists(tree_fn): print("Input tree file does not exist: %s" % tree_fn) t = tree.Tree(tree_fn, format=1) CheckTree(t) d = AveDist(t) print("Average distance from root to leaves: %f" % d) for n in t.traverse('preorder'): if n.is_root(): n.dist = 0 continue # work downwards, setting the branch distances from the top down x = t.get_distance(n) - n.dist y = n.dist print("\nTaxa:") print(", ".join(n.get_leaf_names())) if n.is_leaf(): z = 0. else: z = AveDist(n) print("Distance of parent node from root: %f" % x) print("Current branch length: %f" % y) print("Average distance to leaves: %f" % z) if (y+z) == 0.: n.dist = 0 else: f = (d-x)/(y + z) n.dist = f * n.dist print("Branch length for ultrametric tree: %f" % n.dist) outfn = tree_fn + ".ultrametric.tre" t.write(outfile=outfn, format=5) print("\nUltrametric tree written to: %s\n" % outfn)
def ReplaceFileWithNewIDs(idsMap, treeFilename, newTreeFilename): t = tree.Tree(treeFilename, format=1) for node in t.get_leaves(): node.name = idsMap[node.name] outputTreeString = t.write(format=1) with open(newTreeFilename, 'wb') as outFile: outFile.write(outputTreeString)
def ReplaceFileWithNewIDs(idsMap, treeFilename, newTreeFilename): qHaveSupport = False qHaveInternalNames = False try: t = tree.Tree(treeFilename, format=2) qHaveSupport = True except: try: t = tree.Tree(treeFilename, format=3) qHaveInternalNames = True except: t = tree.Tree(treeFilename) for node in t.get_leaves(): node.name = idsMap[node.name] if qHaveSupport: t.write(outfile=newTreeFilename) elif qHaveInternalNames: t.write(outfile=newTreeFilename, format=3) else: t.write(outfile=newTreeFilename, format=5)