def mylayout(node): profileFace = ProfileFace(matrix_max, matrix_min, matrix_avg, \ 200, 14, "heatmap") cbarsFace = ProfileFace(matrix_max,matrix_min,matrix_avg,200,70,"cbars") # If node is a leaf if node.is_leaf(): # And a line profile add_face_to_node(profileFace, node, 0, aligned=True) node.img_style["size"]=0 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: validationFace = TextFace("Silh=%0.2f" %node.silhouette, "Verdana", 10, "#056600") node.img_style["fgcolor"]="#056600" # Otherwise, use red bubbles else: validationFace = TextFace("Silh=%0.2f" %node.silhouette, "Verdana", 10, "#940000") node.img_style["fgcolor"]="#940000" # Sets node size proportional to the silhouette value. node.img_style["shape"]="sphere" if node.silhouette<=1 and node.silhouette>=-1: node.img_style["size"]= 15+int((abs(node.silhouette)*10)**2) # If node is very internal, draw also a bar diagram # with the average expression of the partition add_face_to_node(validationFace, node, 0) if len(node)>100: add_face_to_node(cbarsFace, node, 1)
def ColorCodedNode (node): if node.is_leaf(): ColorCode=PhenoDict[node.name] if ColorCode == '1': #Name=faces.AttrFace('name',fsize='20',fgcolor="Blue") #NameFace=TextFace(Name) faces.add_face_to_node(AttrFace('name',fsize=20,fgcolor='blue'), node, column=0,aligned=True) #faces.add_face_to_node(TextFace(text='marker1',fsize=10,fgcolor='black'), node, column=1,position='aligned') faces.add_face_to_node(ProfileFace(1, -1, 0, width=200, height=40, style='heatmap', colorscheme=2),node,column=1,position='aligned') elif ColorCode == '2': #Name=faces.AttrFace('name',fsize='20',fgcolor="Red") #NameFace=TextFace(Name) faces.add_face_to_node(AttrFace("name",fsize=20,fgcolor='red'), node, column=0,aligned=True) faces.add_face_to_node(ProfileFace(1, -1, 0, width=200, height=40, style='heatmap', colorscheme=2),node,column=1,position='aligned') elif ColorCode == '-9': faces.add_face_to_node(AttrFace("name",fsize=20,fgcolor='black'), node, column=0,aligned=True) faces.add_face_to_node(ProfileFace(1, -1, 0, width=200, height=40, style='heatmap', colorscheme=2),node,column=1,position='aligned')
def mylayout(node): if node.is_leaf(): if node.name == "Danio_rerio": # import pudb; pudb.set_trace() pass # profile_face = ProfileFace(data_max[1], data_min[1], 0.0, 100, 14, "heatmap") profile_face = ProfileFace(data_max[0], data_min[0], 0.0, 100, 14, "heatmap") ete3.treeview.faces.add_face_to_node(profile_face, node, 0, aligned=True)
def create_circle(self, filename, title, name2color=None, name2class_dic=None, class2color_dic=None, vector=None, ignore_branch_length=True): plt.clf() axis_font = {'size': '3'} plt.rc('xtick', labelsize=0.1) plt.rc('ytick', labelsize=0.1) plt.rc({'font.size': 0.1}) # legend creation if name2class_dic and class2color_dic: leg = [] for cls, color in class2color_dic.items(): leg.append(mpatches.Patch(color=color, label=cls)) t = Tree(self.nwk) # iterate over tree leaves only for l in t.iter_leaves(): ns = NodeStyle() if name2color: ns["bgcolor"] = name2color[ l.name] if l.name in name2color else 'white' elif name2class_dic and class2color_dic: ns["bgcolor"] = class2color_dic[name2class_dic[ l.name]] if l.name in name2class_dic else 'white' # Gray dashed branch lines #ns["hz_line_type"] = 1 #ns["hz_line_color"] = "#cccccc" # l.img_style = ns F = TextFace(l.name) F.ftype = 'Times' #if vector: # if l.name in vector: l.add_features(profile=[random.random() for x in range(10)]) #vector[l.name]) l.add_features(deviation=[0 for x in range(10) ]) #len(vector[l.name]))]) l.add_face(ProfileFace(max_v=1, min_v=0.0, center_v=0.5, width=200, height=40, style='heatmap', colorscheme=5), column=0, position='aligned') # Create an empty TreeStyle ts = TreeStyle() # Set our custom layout function ts.layout_fn = VisualizeCircularTree.layout # Draw a tree ts.mode = "c" # We will add node names manually ts.show_leaf_name = False # Show branch data ts.show_branch_length = True ts.show_branch_support = True ts.force_topology = ignore_branch_length ts.title.add_face(TextFace(title, fsize=20, ftype='Times'), column=15) # legend creation if name2class_dic and class2color_dic: for k, (cls, col) in enumerate(class2color_dic.items()): x = RectFace(8, 8, 'black', col) #x.opacity=0.5 ts.legend.add_face(x, column=8) ts.legend.add_face(TextFace(' ' + cls + ' ', fsize=9, ftype='Times'), column=9) t.render(filename + '.pdf', tree_style=ts, dpi=5000)
t = ClusterTree(PATH + "diauxic.nw", PATH + "diauxic.array") # nodes are linked to the array table array = t.arraytable # Calculates some stats on the matrix. Needed to establish the color # gradients. 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 = ProfileFace(matrix_max, matrix_min, matrix_avg, \ 200, 14, "heatmap") cbarsFace = ProfileFace(matrix_max, matrix_min, matrix_avg, 200, 70, "cbars") nameFace = 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 add_face_to_node(profileFace, node, 0, aligned=True) node.img_style["size"] = 0 add_face_to_node(nameFace, node, 1, aligned=True) # If node is internal else:
def create_circle(self, filename, title, ignore_branch_length=True): plt.clf() axis_font = {'size': '29'} plt.rc('xtick', labelsize=0.1) plt.rc('ytick', labelsize=0.1) plt.rc({'font.size': 0.2}) plt.rc('text', usetex=True) # legend creation if self.motif2struct and self.struct2color_dic: leg = [] for cls, color in self.struct2color_dic.items(): leg.append(mpatches.Patch(color=color, label=str(cls))) t = Tree(self.nwk) node_to_keep = [] for l in t.iter_leaves(): if len(l.name) > 1: node_to_keep.append(l.name) t.prune(tuple(node_to_keep)) # iterate over tree leaves only for l in t.iter_leaves(): ns = NodeStyle() if self.motif2struct and self.struct2color_dic: ns["bgcolor"] = self.struct2color_dic[self.motif2struct[ l. name]] if l.name in self.motif2struct and self.motif2struct[ l.name] in self.struct2color_dic else 'white' # Gray dashed branch lines #ns["hz_line_type"] = 1 #ns["hz_line_color"] = "#cccccc" # l.img_style = ns if self.propVec: if l.name in self.propVec: l.add_features(profile=self.propVec[l.name]) l.add_features(deviation=[ 0 for x in range(len(self.propVec[l.name])) ]) l.add_face(ProfileFace(max_v=1, min_v=-1, center_v=0, width=200, height=40, style='heatmap', colorscheme=2), column=3, position='aligned') l.name = " " + l.name + " " # Create an empty TreeStyle ts = TreeStyle() # Set our custom layout function ts.layout_fn = VisualizeTreeOfMotifs.layout # Draw a tree ts.mode = "c" # We will add node names manually ts.show_leaf_name = False # Show branch data ts.show_branch_length = False ts.show_branch_support = False ts.force_topology = ignore_branch_length ts.title.add_face(TextFace(title, fsize=20, ftype='Times'), column=15) # legend creation if self.motif2struct and self.struct2color_dic: for k, (cls, col) in enumerate(self.struct2color_dic.items()): x = RectFace(15, 15, 'black', col) #x.opacity=0.5 ts.legend.add_face(x, column=8) ts.legend.add_face(TextFace(' ' + str(cls) + ' ', fsize=30, ftype='Times'), column=9) ts.legend.add_face(TextFace('--- Properties vector order ↓', fsize=30, ftype='Times'), column=10) for y in [ 'Mean molecular weight of amino acids', 'Mean flexibility of amino acids', 'Mean DIWV instability index of sequence', 'Mean surface accessibility of amino acids', 'Mean KD hydrophobicity', 'Mean hydrophilicity of amino acids' ]: x = RectFace(5, 5, 'black', 'black') #x.opacity=0.5 ts.legend.add_face(x, column=11) ts.legend.add_face(TextFace(' ' + y + ' ', fsize=25, ftype='Times'), column=12) t.render(filename + '.pdf', tree_style=ts, dpi=5000)
def plot_heat_tree_V0(heatmap_file, tree_file, output_file=None): ''' Plot heatmap next to a tree. The order of the heatmap **MUST** be the same, as order of the leafs on the tree. The tree must be in the Newick format. If *output_file* is specified, then heat-tree will be rendered as a PNG, otherwise interactive browser will pop-up with your heat-tree. Parameters ---------- heatmap_file: str Path to the heatmap file. The first row must have '#Names' as first element of the header. e.g. #Names, A, B, C, D row1, 2, 4, 0, 4 row2, 4, 6, 2, -1 tree_file: str Path to the tree file in Newick format. The leaf node labels should be the same as as row names in the heatmap file. E.g. row1, row2. output_file: str, optional If specified the heat-tree will be rendered in that file as a PNG image, otherwise interactive browser will pop-up. **N.B.** program will wait for you to exit the browser before continuing. ''' import numpy from ete3.treeview.faces import add_face_to_node from ete3 import ClusterTree, TreeStyle, AttrFace, ProfileFace # To operate with numbers efficiently # Loads tree and array t = ClusterTree(tree_file, heatmap_file) t.ladderize() R = t.get_midpoint_outgroup() t.set_outgroup(R) # nodes are linked to the array table array = t.arraytable # Calculates some stats on the matrix. Needed to establish the color # gradients. 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 = ProfileFace(1., 0., 0.5, 1000, 14, "heatmap", colorscheme=1) nameFace = 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 add_face_to_node(profileFace, node, 0, aligned=True) node.img_style["size"] = 0 add_face_to_node(nameFace, node, 1, aligned=True) # Use my layout to visualize the tree ts = TreeStyle() ts.layout_fn = mylayout t.render("test.svg", tree_style=ts) '''