def layout(node): #print(node) if (len(node.get_ancestors()) < 4): print(node.name) n = AttrFace("name", fsize=9) n.margin_top = 10 n.margin_bottom = 0 n.margin_left = 10 faces.add_face_to_node(n, node, 0, position="float") tf = TextFace(len(node.get_leaves()), fsize=12) faces.add_face_to_node(tf, node, 0, position="float")
def ts_layout(node, fgcolor='#FF4ba6', fsize=12): ''' Please set up as the following code shows: ```py ts = TreeStyle() ts.mode = 'r' # 'c' ts.min_leaf_separation = 40 # ts.branch_vertical_margin = 20 # Do not add leaf names automatically ts.show_leaf_name = False ts.show_scale = False ts.optimal_scale_level = 'mid' ts.margin_top = 5 ts.margin_left = 5 ts.margin_right = 5 # Use my custom layout ts.layout_fn = ts_layout ``` ''' node.img_style["size"] = 2 node.img_style["shape"] = "circle" node.img_style["fgcolor"] = fgcolor if node.is_leaf(): # If terminal node, draws its name name_face = AttrFace("name", fsize=fsize) # fgcolor="royalblue" name_face.margin_left = 3 faces.add_face_to_node(name_face, node, column=0, position="branch-right") elif not node.is_root(): # If internal node, draws label with smaller font size name_face = AttrFace("name", fsize=fsize) name_face.margin_left = 2 name_face.margin_bottom = 2 # Adds the name face to the image at the preferred position faces.add_face_to_node(name_face, node, column=0, position="branch-top")
def ete_layout(node): """ Formatting of tree nodes while tree is rendered :param node: ete node """ nstyle = NodeStyle() nstyle["shape"] = "sphere" nstyle["size"] = 0 nstyle["vt_line_width"] = 1 # line width of vertical branches nstyle["hz_line_width"] = 1 # line width of horizontal branches node.set_style(nstyle) if 'median_vaf' in node.features and not np.isnan( node.median_vaf) and logger.isEnabledFor(logging.DEBUG): # # Creates a sphere face whose size is proportional to the given VAF or CCF of the acquired muts # cf = CircleFace(radius=node.median_vaf*50, color="RoyalBlue", style="sphere") # # Let's make the sphere transparent # cf.opacity = 0.6 # # And place as a float face over the tree # faces.add_face_to_node(cf, node, column=0, position="float-behind") vaf_face = TextFace('({:.0%}) '.format(node.median_vaf), fsize=9, fgcolor='SlateGray') vaf_face.opacity = 0.8 # from 0 to 1 faces.add_face_to_node(vaf_face, node, column=2, position="branch-top") if node.is_root(): # tf_root = TextFace(node.name, fsize=14, fgcolor="Black") # tf_root.margin_bottom = 25 # faces.add_face_to_node(tf_root, node, column=0, position='branch-right') return bt_face_muts = AttrFace("dist", fsize=11, fgcolor="Black", text_prefix=' ', text_suffix=' ', formatter='%d') bt_face_muts.margin_bottom = 2 bt_face_muts.margin_top = 2 faces.add_face_to_node(bt_face_muts, node, column=1, position="branch-top") if 'drivers' in node.features and len(node.drivers) > 0: drivers_face = TextFace(node.drivers, fsize=11, fstyle='italic', fgcolor='OrangeRed') drivers_face.opacity = 0.8 # from 0 to 1 drivers_face.margin_left = 1 drivers_face.margin_right = 1 faces.add_face_to_node(drivers_face, node, column=0, position="branch-top") # If node is a leaf, add the nodes name and a its scientific name if node.is_leaf(): # for colors see: http://etetoolkit.org/docs/latest/reference/reference_treeview.html#color-names leaf_face = AttrFace("name", fsize=14, fgcolor="Black", text_prefix=' ', text_suffix=' ') if node.name.startswith('PT') or node.name.startswith( 'Primary'): # primary tumor sample leaf_face.fgcolor = 'SteelBlue' elif node.name.startswith('LiM'): # liver met leaf_face.fgcolor = 'DarkOliveGreen' elif node.name.startswith('LuM'): # lung met leaf_face.fgcolor = 'SaddleBrown' elif node.name.startswith('NoM'): # lymph node met leaf_face.fgcolor = 'DarkViolet' elif node.name.startswith('PeM'): # peritoneal met leaf_face.fgcolor = 'Purple' elif node.name.startswith('Met') or node.name.startswith( 'M'): # some metastasis leaf_face.fgcolor = 'Magenta' elif node.name.startswith('BrM'): # brain met leaf_face.fgcolor = 'Crimson' leaf_face.border.type = 0 leaf_face.border.width = 1 leaf_face.margin_bottom = 1 leaf_face.margin_top = 1 faces.add_face_to_node(leaf_face, node, column=1) # , position="aligned" else: # inner node # if not np.isnan(node.support): # bt_face_conf = AttrFace("support", fsize=12, fgcolor="DimGrey", text_prefix=' ', # text_suffix=' ', formatter='%0.2f') if node.confidence is not None and node.confidence != '': tf_conf = TextFace(node.confidence, fsize=12, fgcolor='DimGrey') tf_conf.margin_left = 3 tf_conf.margin_right = 3 faces.add_face_to_node(tf_conf, node, column=1, position="branch-bottom")