def layout(node): if node.is_leaf(): if "Russia/" in node.name: Rudesc = faces.AttrFace("name", fsize=10) Rudesc.margin_left = 25 faces.add_face_to_node(Rudesc, node, 0, aligned=True) else: if "_" in node.name: node.name = node.name.replace("_", " ") countrydesc = faces.AttrFace("name", fsize=10) countrydesc.margin_left = 5 countrydesc.margin_bottom = 10 countrydesc.margin_top = 5 countrydesc.margin_right = 5 faces.add_face_to_node(countrydesc, node, 0, aligned=False)
def set_face(ftype, fgcolor, fsize=25): face = faces.AttrFace("name", fsize=fsize, fgcolor=fgcolor, ftype=ftype) face.margin_left = 2 return face
# Creates a random tree with 8 leaves using a given set of names # t.populate(8, ["Dme", "Dre", "Hsa", "Ptr", "Cfa", "Mms"]) # Set the path in which images are located img_path = "./" # Create faces based on external images humanFace = faces.ImgFace(img_path + "human.png") mouseFace = faces.ImgFace(img_path + "mouse.png") dogFace = faces.ImgFace(img_path + "dog.png") chimpFace = faces.ImgFace(img_path + "chimp.png") fishFace = faces.ImgFace(img_path + "fish.png") flyFace = faces.ImgFace(img_path + "fly.png") # Create a faces ready to read the name attribute of nodes #nameFace = faces.TextFace(open("text").readline().strip(), fsize=20, fgcolor="#009000") nameFace = faces.AttrFace("name", fsize=20, fgcolor="#009000") # Create a conversion between leaf names and real names code2name = { "Dre": "Drosophila melanogaster", "Dme": "Danio rerio", "Hsa": "H**o sapiens", "Ptr": "Pan troglodytes", "Mms": "Mus musculus", "Cfa": "Canis familiaris" } # Creates a dictionary with the descriptions of each leaf name code2desc = { "Dre": """
def tree_renderer(tree, treeid, application): # The following part controls the features that are attched to # leaf nodes and that will be shown in the tree image. Node styles # are set it here, and faces are also created. The idea is the # following: user can pass feature names using the URL argument # "tree_features". If the feature is handled by our function and # it is available in nodes, a face will be created and added to # the global variable LEAVE_FACES. Remember that our layout # function uses such variable to add faces to nodes during # rendering. # Extracts from URL query the features that must be drawn in the tree asked_features = application.queries.get("show_features", ["name"])[0].split(",") print >> sys.stderr, asked_features def update_features_avail(feature_key, name, col, fsize, fcolor, prefix, suffix): text_features_avail.setdefault( feature_key, [name, 0, col, fsize, fcolor, prefix, suffix]) text_features_avail[feature_key][1] += 1 tree.add_feature("fgcolor", "#833DB4") tree.add_feature("shape", "sphere") tree.add_feature("bsize", "8") tree.dist = 0 # This are the features that I wanto to convert into image # faces. I use an automatic function to do this. Each element in # the dictionary is a list that contains the information about how # to create a textFace with the feature. leaves = tree.get_leaves() formated_features = { # feature_name: ["Description", face column position, text_size, color, text_prefix, text_suffix ] "name": ["Leaf name", len(leaves), 0, 12, "#000000", "", ""], "spname": ["Species name", len(leaves), 1, 12, "#f00000", " Species:", ""], } # populates the global LEAVE_FACES variable global LEAVE_FACES LEAVE_FACES = [] unknown_faces_pos = 2 for fkey in asked_features: if fkey in formated_features: name, total, pos, size, color, prefix, suffix = formated_features[ fkey] f = faces.AttrFace(fkey, ftype="Arial", fsize=size, fgcolor=color, text_prefix=prefix, text_suffix=suffix) LEAVE_FACES.append([f, fkey, pos]) else: # If the feature has no associated format, let's add something standard prefix = " %s: " % fkey f = faces.AttrFace(fkey, ftype="Arial", fsize=10, fgcolor="#666666", text_prefix=prefix, text_suffix="") LEAVE_FACES.append([f, fkey, unknown_faces_pos]) unknown_faces_pos += 1 text_features_avail = {} for l in leaves: for f in l.features: if not f.startswith("_"): text_features_avail.setdefault(f, 0) text_features_avail[f] = text_features_avail[f] + 1 html_features = """ <div id="tree_features_box"> <div class="tree_box_header">Available tree features <img src="/webplugin/close.png" onclick='$(this).closest("#tree_features_box").hide();'> </div> <form action='javascript: set_tree_features("", "", "");'> """ for fkey, counter in text_features_avail.iteritems(): if fkey in asked_features: tag = "CHECKED" else: tag = "" fname = formated_features.get(fkey, [fkey])[0] #html_features = "<tr>" html_features += '<INPUT NAME="tree_feature_selector" TYPE=CHECKBOX %s VALUE="%s">%s (%s/%s leaves)</input><br> ' %\ (tag, fkey, fname, counter, len(leaves)) # html_features += '<td><INPUT size=7 type="text"></td> <td><input size=7 type="text"></td> <td><input size=7 type="text"></td> <td><input size=1 type="text"></td><br>' #html_features += "</tr>" html_features += """<input type="submit" value="Refresh" onclick='javascript: // This piece of js code extracts the checked features from menu and redraw the tree sending such information var allVals = []; $(this).parent().children("input[name=tree_feature_selector]").each(function(){ if ($(this).is(":checked")){ allVals.push($(this).val()); }}); draw_tree("%s", "", "#img1", {"show_features": allVals.join(",")} );' > </form></div>""" % (treeid) features_button = """ <li><a href="#" onclick='show_box(event, $(this).closest("#tree_panel").children("#tree_features_box"));'> <img width=16 height=16 src="/webplugin/icon_tools.png" alt="Select Tree features"> </a></li>""" download_button = """ <li><a href="/webplugin/tmp/%s.png" target="_blank"> <img width=16 height=16 src="/webplugin/icon_attachment.png" alt="Download tree image"> </a></li>""" % (treeid) search_button = """ <li><a href="#" onclick='javascript: var box = $(this).closest("#tree_panel").children("#search_in_tree_box"); show_box(event, box); '> <img width=16 height=16 src="/webplugin/icon_search.png" alt="Search in tree"> </a></li>""" clean_search_button = """ <li><a href="#" onclick='run_action("%s", "", %s, "clean::clean");'> <img width=16 height=16 src="/webplugin/icon_cancel_search.png" alt="Clear search results"> </a></li>""" %\ (treeid, 0) buttons = '<div id="ete_tree_buttons">' +\ features_button + search_button + clean_search_button + download_button +\ '</div>' search_select = '<select id="ete_search_target">' for fkey in text_features_avail: search_select += '<option value="%s">%s</option>' % (fkey, fkey) search_select += '</select>' search_form = """ <div id="search_in_tree_box"> <div class="tree_box_header"> Search in Tree <img src="/webplugin/close.png" onclick='$(this).closest("#search_in_tree_box").hide();'> </div> <form onsubmit='javascript: search_in_tree("%s", "%s", $(this).closest("form").children("#ete_search_term").val(), $(this).closest("form").children("#ete_search_target").val());' action="javascript:void(0);"> %s <input id="ete_search_term" type="text" value=""'></input> <br><i>(Searches are not case sensitive and accept Perl regular expressions)</i> <br> </form> <i> (Press ENTER to initiate the search)</i> </div> """ %\ (treeid, 0, search_select) # 0 is the action index associated # to the search functionality. This # means that this action is the # first to be registered in WebApplication. tree_panel_html = '<div id="tree_panel">' + search_form + html_features + buttons + '</div>' # Now we render the tree into image and get the HTML that handles it tree_html = application._get_tree_img(treeid=treeid) # Let's return enriched HTML return tree_panel_html + tree_html
def layout(node): name_face = faces.AttrFace("name", fsize=10, fgcolor="#009000") # If node is a leaf, add the node name if node.is_leaf(): faces.add_face_to_node(name_face, node, column=0) faces.add_face_to_node(AttrFace("node_id"), node, column=0)
def test(node): if node.is_leaf(): faces.add_face_to_node(faces.AttrFace("name"), node, 0, position="aligned")
def leaf_name(node): if node.is_leaf(): nameF = faces.AttrFace("name") nameF.border.width = 1 faces.add_face_to_node(nameF, node, 0, position="branch-right")
def get_example_tree(): t = Tree() t.populate(10) # Margins, alignment, border, background and opacity can now be set for any face rs1 = faces.TextFace("branch-right\nmargins&borders", fsize=12, fgcolor="#009000") rs1.margin_top = 10 rs1.margin_bottom = 50 rs1.margin_left = 40 rs1.margin_right = 40 rs1.border.width = 1 rs1.background.color = "lightgreen" rs1.inner_border.width = 0 rs1.inner_border.line_style = 1 rs1.inner_border.color = "red" rs1.opacity = 0.6 rs1.hz_align = 2 # 0 left, 1 center, 2 right rs1.vt_align = 1 # 0 left, 1 center, 2 right br1 = faces.TextFace("branch-right1", fsize=12, fgcolor="#009000") br2 = faces.TextFace("branch-right3", fsize=12, fgcolor="#009000") # New face positions (branch-top and branch-bottom) bb = faces.TextFace("branch-bottom 1", fsize=8, fgcolor="#909000") bb2 = faces.TextFace("branch-bottom 2", fsize=8, fgcolor="#909000") bt = faces.TextFace("branch-top 1", fsize=6, fgcolor="#099000") # And faces can also be used as headers or foot notes of aligned # columns t1 = faces.TextFace("Header Face", fsize=12, fgcolor="#aa0000") t2 = faces.TextFace("Footer Face", fsize=12, fgcolor="#0000aa") # Attribute faces can now contain prefix and suffix fixed text aligned = faces.AttrFace("name", fsize=12, fgcolor="RoyalBlue", text_prefix="Aligned (", text_suffix=")") # horizontal and vertical alignment per face aligned.hz_align = 1 # 0 left, 1 center, 2 right aligned.vt_align = 1 # Node style handling is no longer limited to layout functions. You # can now create fixed node styles and use them many times, save them # or even add them to nodes before drawing (this allows to save and # reproduce an tree image design) style = NodeStyle() style["fgcolor"] = "Gold" style["shape"] = "square" style["size"] = 15 style["vt_line_color"] = "#ff0000" t.set_style(style) # add a face to the style. This face will be render in any node # associated to the style. fixed = faces.TextFace("FIXED branch-right", fsize=11, fgcolor="blue") t.add_face(fixed, column=1, position="branch-right") # Bind the precomputed style to the root node # ETE 2.1 has now support for general image properties ts = TreeStyle() # You can add faces to the tree image (without any node # associated). They will be used as headers and foot notes of the # aligned columns (aligned faces) ts.aligned_header.add_face(t1, column=0) ts.aligned_header.add_face(t1, 1) ts.aligned_header.add_face(t1, 2) ts.aligned_header.add_face(t1, 3) t1.hz_align = 1 # 0 left, 1 center, 2 right t1.border.width = 1 ts.aligned_foot.add_face(t2, column=0) ts.aligned_foot.add_face(t2, 1) ts.aligned_foot.add_face(t2, 2) ts.aligned_foot.add_face(t2, 3) t2.hz_align = 1 # Set tree image style. Note that aligned header and foot is only # visible in "rect" mode. ts.mode = "r" ts.scale = 10 for node in t.traverse(): # If node is a leaf, add the nodes name and a its scientific # name if node.is_leaf(): node.add_face(aligned, column=0, position="aligned") node.add_face(aligned, column=1, position="aligned") node.add_face(aligned, column=3, position="aligned") else: node.add_face(bt, column=0, position="branch-top") node.add_face(bb, column=0, position="branch-bottom") node.add_face(bb2, column=0, position="branch-bottom") node.add_face(br1, column=0, position="branch-right") node.add_face(rs1, column=0, position="branch-right") node.add_face(br2, column=0, position="branch-right") return t, ts
elif opt == '-u': ultrametric = int(arg) elif opt == '-s': support_threshold = float(arg) elif opt == '-i': show_IDs = int(arg) gene_tree = args[0] print(gene_tree) print(gene_format) gene_tree = Tree(gene_tree, format=gene_format) #gene_tree = ete2.Tree("/home/ciach/Projects/Tree_node_classification/Comparison_with_Notung/Sequences/ABV48733.1/decomposed_tree.dnd") #species_tree = ete2.Tree("/home/ciach/Projects/Tree_node_classification/Comparison_with_Notung/Sequences/ABV48733.1/species_tree.dnd") suppFace = faces.AttrFace("support", text_suffix=" ", formatter="%.2f", fsize=8) iFace = faces.AttrFace("I", fsize=8, text_suffix='/') pFace = faces.AttrFace("P", fsize=8, text_suffix=' ') idFace = faces.AttrFace("id", fsize=8) for i, g in enumerate(gene_tree.traverse(strategy="postorder")): g.id = i #g.name = g.name.split('_')[0] g.coloured = False g.nw = g.source g.source = True if g.source == 'True' else False g.dist = 1.0 if hasattr(g, 'I'): if g.I == "None": g.I = -1 else:
array = t.arraytable # Calculates some stats on the matrix matrix_dist = [i for r in xrange(len(array.matrix))\ for i in array.matrix[r] if numpy.isfinite(i)] matrix_max = numpy.max(matrix_dist) matrix_min = numpy.min(matrix_dist) matrix_avg = matrix_min + ((matrix_max - matrix_min) / 2) # Creates a profile face that will represent node's profile as a # heatmap profileFace = faces.ProfileFace(matrix_max, matrix_min, matrix_avg, \ 200, 14, "heatmap") cbarsFace = faces.ProfileFace(matrix_max, matrix_min, matrix_avg, 200, 70, "cbars") nameFace = faces.AttrFace("name", fsize=8) # Creates my own layout function that uses previous faces def mylayout(node): # If node is a leaf if node.is_leaf(): # And a line profile faces.add_face_to_node(profileFace, node, 0, aligned=True) node.img_style["size"] = 0 faces.add_face_to_node(nameFace, node, 1, aligned=True) # If node is internal else: # If silhouette is good, creates a green bubble if node.silhouette > 0: