for n in node.postiter(): c = n2coords[n] if not n.isleaf: children = n.children v = [n2coords[children[0]].y, n2coords[children[-1]].y] v.sort() ymin, ymax = v c.y = (ymax + ymin)/2.0 if not scaled: c.x = min([ n2coords[ch].x for ch in children ]) - 1.0 else: c.x = c.length_to_root if not scaled: for i in range(smooth): smooth_xpos(node, n2coords) return n2coords if __name__ == "__main__": import tree node = tree.read("(a:3,(b:2,(c:4,d:5):1,(e:3,(f:1,g:1):2):2):2);") for i, n in enumerate(node.iternodes()): if not n.isleaf: n.label = "node%s" % i node.label = "root" n2c = calc_node_positions(node, width=10, height=10, scaled=True) from pprint import pprint pprint(n2c)
## 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]
PIC(child, data, results) child_results = results[child] X.append(child_results[0]) v.append(child_results[1]) else: X.append(data[child.label]) v.append(child.length) Xi, Xj = X # Xi - Xj is the contrast value vi, vj = v # 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
if node.isleaf: buf.putstr(nc.r, nc.c+1, " "+node.label) else: if node.label and show_internal_labels: buf.putstr(nc.r, nc.c-len(node.label), node.label) buf.putstr(nc.r, nc.c, "+") return str(buf) if __name__ == "__main__": import random, tree rand = random.Random() t = tree.read( "(foo,((bar,(dog,cat)dc)dcb,(shoe,(fly,(cow, bowwow)cowb)cbf)X)Y)Z;" ) #t = tree.read("(((foo:4.6):5.6, (bar:6.5, baz:2.3):3.0):3.0);") #t = tree.read("(foo:4.6, (bar:6.5, baz:2.3)X:3.0)Y:3.0;") i = 1 print render(t, scaled=0, show_internal_labels=1) r = t.get("cat").parent tree.reroot(t, r) tp = t.parent tp.remove_child(t) c = t.children[0] t.remove_child(c) tp.add_child(c) print render(r, scaled=0, show_internal_labels=1)
Xi, Xj = X # Xi - Xj is the contrast value vi, vj = v # 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)