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
def style_nodes(root, request, selected_node_id=None, wscale=1.0, scaled=True): bgcolor = "black" selcolor = "red" leaves = root.leaves() l2d = root.leaf_distances(measure=phylo.INTERNODES)[root.id] height = len(leaves) * 3 unit = 3.0 width = max([l2d[lf.id] for lf in leaves]) * unit * wscale width = min(width, 65) rpad = max([len(lf.label or "") for lf in leaves]) * 0.7 lpad = max(1, len(root.label or []) * 0.7) width += rpad + 2 + lpad branchwidth = 0.75 n2c = layout.calc_node_positions(root, width, height, lpad=lpad + 1, tpad=1, bpad=2.5, rpad=rpad + 1, scaled=scaled) n2c[root].px = 1 for node in root.iternodes(): coords = n2c[node] w = coords.x - coords.px node.hbranch_style["width"] = w node.hbranch_style["height"] = branchwidth node.hbranch_style["top"] = coords.y + 0.5 node.hbranch_style["left"] = coords.px node.hbranch_style["background-color"] = bgcolor if coords.py is None: coords.py = coords.y if coords.py < coords.y: h = coords.y - coords.py y = coords.py else: h = coords.py - coords.y y = coords.y node.vbranch_style["width"] = 0.5 * branchwidth node.vbranch_style["height"] = h node.vbranch_style["top"] = y + 0.5 node.vbranch_style["left"] = coords.px node.vbranch_style["background-color"] = bgcolor if node.istip: node.label_style["top"] = coords.y - 0.5 node.label_style["left"] = coords.x + 0.25 node.label_style["width"] = len(node.label or "") node.label_style["text-align"] = "left" else: node.label_style["text-align"] = "right" node.label_style["width"] = len(node.label or "") node.label_style["top"] = coords.y - 0.5 node.label_style["left"] = coords.x - len(node.label or "") node.ref_style["top"] = coords.y - 0.5 node.ref_style["left"] = coords.x + 0.25 if selected_node_id: for n in root.iternodes(): if n.id == selected_node_id: for m in n.iternodes(): m.vbranch_style["background-color"] = selcolor m.hbranch_style["background-color"] = selcolor break return width, height
def style_nodes(root, collapsed=None, selected_node_id=None, wscale=1.0): style = defaultdict(Storage) collapsed = collapsed or set() bgcolor = "gray" selcolor = "red" leaves = getleaves(root, collapsed=collapsed) l2d = leaf_distances(root, collapsed=collapsed) height = len(leaves) * 2.5 unit = 3.0 width = max([l2d[lf] for lf in leaves]) * unit * wscale width = min(width, 65) rpad = max([len(lf.label or "") for lf in leaves]) * 0.7 lpad = max(1, len(root.label or []) * 0.7) width += rpad + 2 + lpad branchwidth = 0.75 n2c = layout.calc_node_positions( root, width, height, lpad=lpad + 1, tpad=1, bpad=2.5, rpad=rpad + 1, collapsed=collapsed, scaled=False ) n2c[root].px = 1 for node in root.iternodes(f=lambda x: x.rec.id not in collapsed): coords = n2c[node] style[node].x = coords.x style[node].y = coords.y w = coords.x - coords.px pos = "absolute" style[node].hbranch = Storage() hbranch = style[node].hbranch hbranch["position"] = pos hbranch["width"] = w hbranch["height"] = branchwidth hbranch["top"] = coords.y + 0.5 hbranch["left"] = coords.px hbranch["background-color"] = bgcolor if float(node.rec.bootstrap_support or 0) > 0.70: hbranch["background-color"] = "green" if coords.py is None: coords.py = coords.y if coords.py < coords.y: h = coords.y - coords.py y = coords.py else: h = coords.py - coords.y y = coords.y style[node].vbranch = Storage() vbranch = style[node].vbranch vbranch["position"] = pos vbranch["width"] = 0.5 * branchwidth vbranch["height"] = h vbranch["top"] = y + 0.5 vbranch["left"] = coords.px vbranch["background-color"] = bgcolor style[node].label = Storage() label = style[node].label label["position"] = pos if node.isleaf or node.rec.id in collapsed: label["top"] = coords.y - 0.75 label["left"] = coords.x + 0.25 if node.rec.otu and node.rec.otu.taxon: s = node.rec.otu.taxon.name label["width"] = len(s) else: label["width"] = len(node.label or "") label["text-align"] = "left" else: label["text-align"] = "right" label["width"] = len(node.label or "") label["top"] = coords.y - 0.75 label["left"] = coords.x - len(str(node.label or "")) style[node].ref = Storage() ref = style[node].ref ref["position"] = pos ref["top"] = coords.y - 0.5 ref["left"] = coords.x + 0.25 if selected_node_id: for n in root.iternodes(f=lambda x: x.rec.id not in collapsed): if n.rec.id == selected_node_id: for m in n.iternodes(): style[m].vbranch["background-color"] = selcolor style[m].hbranch["background-color"] = selcolor break return width, height, style
def style_nodes(root, collapsed=None, selected_node_id=None, wscale=1.0): style = defaultdict(Storage) collapsed = collapsed or set() bgcolor = "gray" selcolor = "red" leaves = getleaves(root, collapsed=collapsed) l2d = leaf_distances(root, collapsed=collapsed) height = len(leaves) * 2.5 unit = 3.0 width = max([l2d[lf] for lf in leaves]) * unit * wscale width = min(width, 65) rpad = max([len(lf.label or "") for lf in leaves]) * 0.7 lpad = max(1, len(root.label or []) * 0.7) width += rpad + 2 + lpad branchwidth = 0.75 n2c = layout.calc_node_positions(root, width, height, lpad=lpad + 1, tpad=1, bpad=2.5, rpad=rpad + 1, collapsed=collapsed, scaled=False) n2c[root].px = 1 for node in root.iternodes(f=lambda x: x.rec.id not in collapsed): coords = n2c[node] style[node].x = coords.x style[node].y = coords.y w = coords.x - coords.px pos = "absolute" style[node].hbranch = Storage() hbranch = style[node].hbranch hbranch["position"] = pos hbranch["width"] = w hbranch["height"] = branchwidth hbranch["top"] = coords.y + 0.5 hbranch["left"] = coords.px hbranch["background-color"] = bgcolor if float(node.rec.bootstrap_support or 0) > 0.70: hbranch["background-color"] = "green" if coords.py is None: coords.py = coords.y if coords.py < coords.y: h = coords.y - coords.py y = coords.py else: h = coords.py - coords.y y = coords.y style[node].vbranch = Storage() vbranch = style[node].vbranch vbranch["position"] = pos vbranch["width"] = 0.5 * branchwidth vbranch["height"] = h vbranch["top"] = y + 0.5 vbranch["left"] = coords.px vbranch["background-color"] = bgcolor style[node].label = Storage() label = style[node].label label["position"] = pos if node.isleaf or node.rec.id in collapsed: label["top"] = coords.y - 0.75 label["left"] = coords.x + 0.25 if node.rec.otu and node.rec.otu.taxon: s = node.rec.otu.taxon.name label["width"] = len(s) else: label["width"] = len(node.label or "") label["text-align"] = "left" else: label["text-align"] = "right" label["width"] = len(node.label or "") label["top"] = coords.y - 0.75 label["left"] = coords.x - len(str(node.label or "")) style[node].ref = Storage() ref = style[node].ref ref["position"] = pos ref["top"] = coords.y - 0.5 ref["left"] = coords.x + 0.25 if selected_node_id: for n in root.iternodes(f=lambda x: x.rec.id not in collapsed): if n.rec.id == selected_node_id: for m in n.iternodes(): style[m].vbranch["background-color"] = selcolor style[m].hbranch["background-color"] = selcolor break return width, height, style
svg = fig.SVG() vb = "%s %s %s %s" % (0, 0, w, h) svgfig.canvas(svg, width=w, height=h, viewBox=vb).save(fname) #svg.save(fname) width = float(commands.getoutput("inkscape -W '%s'" % fname)) height = float(commands.getoutput("inkscape -H '%s'" % fname)) os.remove(fname) return width, height w = 400.0 h = 800.0 r = newick.parse(file("test.newick").read()) r.order_subtrees_by_size() n2c = layout.calc_node_positions(r, w, h, scaled=True) ntips = float(len(r.leaves())) fontsize = h / ntips ## bbox = svgfig.Rect(1,1,w-1,h-1) branches = [] labels = [] for n, c in n2c.items(): if n.parent: pc = n2c[n.parent] d = ((c.x, c.y), (pc.x, c.y), (pc.x, pc.y)) line = svgfig.Poly(d) branches.append(line) if n.label:
fname = tempfile.mktemp(suffix=".svg") svg = fig.SVG() vb = "%s %s %s %s" % (0, 0, w, h) svgfig.canvas(svg, width=w, height=h, viewBox=vb).save(fname) #svg.save(fname) width = float(commands.getoutput("inkscape -W '%s'" % fname)) height = float(commands.getoutput("inkscape -H '%s'" % fname)) os.remove(fname) return width, height w = 400.0 h = 800.0 r = newick.parse(file("test.newick").read()) r.order_subtrees_by_size() n2c = layout.calc_node_positions(r, w, h, scaled=True) ntips = float(len(r.leaves())) fontsize = h/ntips ## bbox = svgfig.Rect(1,1,w-1,h-1) branches = [] labels = [] for n, c in n2c.items(): if n.parent: pc = n2c[n.parent] d = ((c.x, c.y), (pc.x, c.y), (pc.x, pc.y)) line = svgfig.Poly(d) branches.append(line) if n.label:
def style_nodes(root, request, selected_node_id=None, wscale=1.0, scaled=True): bgcolor = "black" selcolor = "red" leaves = root.leaves() l2d = root.leaf_distances(measure=phylo.INTERNODES)[root.id] height = len(leaves)*3 unit = 3.0 width = max([ l2d[lf.id] for lf in leaves ]) * unit * wscale width = min(width, 65) rpad = max([ len(lf.label or "") for lf in leaves ]) * 0.7 lpad = max(1, len(root.label or []) * 0.7) width += rpad+2 + lpad branchwidth = 0.75 n2c = layout.calc_node_positions( root, width, height, lpad=lpad+1, tpad=1, bpad=2.5, rpad=rpad+1, scaled=scaled ) n2c[root].px = 1 for node in root.iternodes(): coords = n2c[node] w = coords.x-coords.px node.hbranch_style["width"] = w node.hbranch_style["height"] = branchwidth node.hbranch_style["top"] = coords.y+0.5 node.hbranch_style["left"] = coords.px node.hbranch_style["background-color"] = bgcolor if coords.py is None: coords.py = coords.y if coords.py < coords.y: h = coords.y-coords.py y = coords.py else: h = coords.py-coords.y y = coords.y node.vbranch_style["width"] = 0.5 * branchwidth node.vbranch_style["height"] = h node.vbranch_style["top"] = y+0.5 node.vbranch_style["left"] = coords.px node.vbranch_style["background-color"] = bgcolor if node.istip: node.label_style["top"] = coords.y-0.5 node.label_style["left"] = coords.x+0.25 node.label_style["width"] = len(node.label or "") node.label_style["text-align"] = "left" else: node.label_style["text-align"] = "right" node.label_style["width"] = len(node.label or "") node.label_style["top"] = coords.y-0.5 node.label_style["left"] = coords.x-len(node.label or "") node.ref_style["top"] = coords.y-0.5 node.ref_style["left"] = coords.x+0.25 if selected_node_id: for n in root.iternodes(): if n.id == selected_node_id: for m in n.iternodes(): m.vbranch_style["background-color"] = selcolor m.hbranch_style["background-color"] = selcolor break return width, height