def test(): import newick, ascii n = newick.parse("(((a:1,b:2):3,(c:3,d:1):1,(e:0.5,f:3):2.5):1,g:4);") v = ltt(n) print ascii.render(n, scaled=1) for t, n in v: print t, n
def ascii_tree(outfile, tree, model, data=None, scaled=False, minwidth=80, tee=False): d = None if data: d = dict([ (x[0], model.dist2label(x[1])) for x in data.items() \ if x[1] in model.dists ]) s = "\n".join( ["Newick tree with interior nodes labeled:", newick.to_string(tree.root)+";", "\n", "Cladogram (branch lengths not to scale):", ascii.render(tree.root, scaled=scaled, minwidth=80, data=d), "\n\n"] ) log(s, outfile, tee)
def ascii(self, *args, **kwargs): """ Create ascii tree. Keyword Args: unitlen (float): How long each unit should be rendered as. Defaults to 3. minwidth (float): Minimum width of the plot. Defaults to 50 maxwidth (float): Maximum width of the plot. Defaults to None scaled (bool): Whether or not the tree is scaled. Defaults to False show_internal_labels (bool): Whether or not to show labels on internal nodes. Defaults to True. Returns: str: Ascii tree to be shown with print(). """ from ascii import render return render(self, *args, **kwargs)
i += 1 return root def ultrametricize(node): import layout n2c = layout.calc_node_positions(node, 1.0, 1.0, scaled=False) for n in node.descendants(): c = n2c[n] n.length = c.x - n2c[n.parent].x if __name__ == "__main__": import newick, ascii, os ## from numpy import array ## #tree = newick.parse("(a,(b,(c,(d,e))));") ## f = os.path.expanduser("~/Projects/pedic-sympatry/matrices/") ## tree = eval(file(f+"garli-ml.tree").read()) ## treespp = tree["species"] ## root = newick.parse(tree["newick"]) ## spp = ['alaschanica', 'cheilanthifolia', 'dichotoma', 'kansuensis', ## 'oederi', 'plicata', 'przewalskii', 'remotiloba', ## 'rhinanthoides', 'roylei', 'rupicola', 'scolopax'] ## print root.subtree_mapping(spp, clean=1) root = bifid_tree(5) ultrametricize(root) print newick.tostring(root) print ascii.render(root) #print node2tipsets(root) print root.mrca(("SP6", "SP10", "SP18")).label
else: results[node] = node_ages[node] for child in node.children: smooth(child, node_ages, results) return results if __name__ == "__main__": import newick, ascii s = "((((a,b),(c,d),(e,f)),g),h);" root = newick.parse(s) leaf_ages = { "a": 3, "b": 2, "c": 4, "d": 1, "e": 3, "f": 0.5, "g": 10, "h": 5, } ma = min_ages(root, leaf_ages) d = ma for i in range(10): d = smooth(root, d) for node, val in ages2lengths(root, d).items(): node.length = val print(ascii.render(root, scaled=1))
def ascii(self, *args, **kwargs): from ascii import render return render(self, *args, **kwargs)
## N = len(labels) ## var = [ [ 0 for x in labels ] for y in labels ] ## cov = [ [ None for x in labels ] for y in labels ] ## d = root.leaf_distances() ## for i in range(N): ## for j in range(i+1, N): ## li = labels[i] ## lj = labels[j] ## for n in root.postiter(): ## l2d = d[n] ## if (not n.isleaf) and (li in l2d) and (lj in l2d): ## dist = l2d[li] + l2d[lj] ## var[i][j] = dist ## cov[i][j] = sum([ x.length for x in n.rootpath() ## if x.parent ]) ## break ## return var, cov if __name__ == "__main__": import tree, ascii from pprint import pprint n = tree.read("(((a:1,b:2):3,(c:3,d:1):1,(e:0.5,f:3):2.5):1,g:4);") var, covar = vcv(n) for x in n: if not x.label: x.label = str(x.length or "") else: x.label = "%s %s" % (x.length, x.label) print ascii.render(n, scaled=1) for k, v in var.items(): print [ x.label.split()[-1] for x in k ], v, covar[k]
results[node] = node_ages[node] for child in node.children: smooth(child, node_ages, results) return results if __name__ == "__main__": import newick, ascii s = "((((a,b),(c,d),(e,f)),g),h);" root = newick.parse(s) leaf_ages = { "a": 3, "b": 2, "c": 4, "d": 1, "e": 3, "f": 0.5, "g": 10, "h": 5, } ma = min_ages(root, leaf_ages) d = ma for i in range(10): d = smooth(root, d) for node, val in ages2lengths(root, d).items(): node.length = val print ascii.render(root, scaled=1)
else: results[node] = node_ages[node] for child in node.children: smooth(child, node_ages, results) return results if __name__ == "__main__": import newick, ascii s = "((((a,b),(c,d),(e,f)),g),h);" root = newick.parse(s) leaf_ages = { "a": 3, "b": 2, "c": 4, "d": 1, "e": 3, "f": 0.5, "g": 10, "h": 5, } ma = min_ages(root, leaf_ages) d = ma for i in range(10): d = smooth(root, d) for node, val in ages2lengths(root, d).items(): node.length = val print ascii.render(root, scaled=1)
results[node] = node_ages[node] for child in node.children: smooth(child, node_ages, results) return results if __name__ == "__main__": import newick, ascii s = "((((a,b),(c,d),(e,f)),g),h);" root = newick.parse(s) leaf_ages = { "a": 3, "b": 2, "c": 4, "d": 1, "e": 3, "f": 0.5, "g": 10, "h": 5, } ma = min_ages(root, leaf_ages) d = ma for i in range(10): d = smooth(root, d) for node, val in ages2lengths(root, d).items(): node.length = val print(ascii.render(root, scaled=1))
else: n.label = "IN%s" % i i += 1 return root def ultrametricize(node): import layout n2c = layout.calc_node_positions(node, 1.0, 1.0, scaled=False) for n in node.descendants(): c = n2c[n] n.length = c.x - n2c[n.parent].x if __name__ == "__main__": import newick, ascii, os ## from numpy import array ## #tree = newick.parse("(a,(b,(c,(d,e))));") ## f = os.path.expanduser("~/Projects/pedic-sympatry/matrices/") ## tree = eval(file(f+"garli-ml.tree").read()) ## treespp = tree["species"] ## root = newick.parse(tree["newick"]) ## spp = ['alaschanica', 'cheilanthifolia', 'dichotoma', 'kansuensis', ## 'oederi', 'plicata', 'przewalskii', 'remotiloba', ## 'rhinanthoides', 'roylei', 'rupicola', 'scolopax'] ## print root.subtree_mapping(spp, clean=1) root = bifid_tree(5) ultrametricize(root) print newick.tostring(root) print ascii.render(root) #print node2tipsets(root) print root.mrca(("SP6","SP10", "SP18")).label
# Xk is the reconstructed state at the node Xk = ((1.0/vi)*Xi + (1/vj)*Xj) / (1.0/vi + 1.0/vj) # vk is the variance vk = node.length + (vi*vj)/(vi+vj) results[node] = (Xk, vk, Xi-Xj, vi+vj) return results if __name__ == "__main__": import tree n = tree.read( "((((H**o:0.21,Pongo:0.21)N1:0.28,Macaca:0.49)N2:0.13,"\ "Ateles:0.62)N3:0.38,Galago:1.00)N4:0.0;" ) char1 = { "H**o": 4.09434, "Pongo": 3.61092, "Macaca": 2.37024, "Ateles": 2.02815, "Galago": -1.46968 } for k, v in PIC(n, char1).items(): print k.label or k.id, v import ascii print ascii.render(n, scaled=True)
# Xk is the reconstructed state at the node Xk = ((1.0 / vi) * Xi + (1 / vj) * Xj) / (1.0 / vi + 1.0 / vj) # vk is the variance vk = node.length + (vi * vj) / (vi + vj) results[node] = (Xk, vk, Xi - Xj, vi + vj) return results if __name__ == "__main__": import tree n = tree.read( "((((H**o:0.21,Pongo:0.21)N1:0.28,Macaca:0.49)N2:0.13,"\ "Ateles:0.62)N3:0.38,Galago:1.00)N4:0.0;" ) char1 = { "H**o": 4.09434, "Pongo": 3.61092, "Macaca": 2.37024, "Ateles": 2.02815, "Galago": -1.46968 } for k, v in PIC(n, char1).items(): print k.label or k.id, v import ascii print ascii.render(n, scaled=True)
## for child in children: ## nstr = nstr+to_string(child, lengths) ## if child != children[-1]: nstr = nstr+',' ## nstr = nstr+')' ## else: ## label = node.label ## nstr = nstr+label ## if (node.length is not None) and lengths: ## nstr = nstr+':'+'%f' % node.length ## return nstr def parse_from_file(filename): if filename == '-': file = sys.stdin else: file = open(filename, 'r') content = string.strip(file.read()) treedescs = string.split(content, ';') tree = parse(treedescs[0]) file.close() return tree if __name__ == "__main__": import ascii s = "(a,(b,c)int)root;" n = parse(s) print print ascii.render(n) print to_string(n) print n.next.back.label