def createImg(filename, thres=0, samples=1): count = parseLineage(filename) suffix, matrix, taxo = getSuffixandMatrixandNewick(count,thres,samples) newick = convert(taxo,suffix) newick += ';' t = Tree(newick, format=1) ct = ClusterTree(t.write(), text_array=matrix) addColors(ct) # nodes are linked to the array table array = ct.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 np.isfinite(i)] matrix_max = np.max(matrix_dist) matrix_min = np.min(matrix_dist) matrix_avg = (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",colorscheme=3) # 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"]=2 # Use my layout to visualize the tree ts = TreeStyle() ts.layout_fn = mylayout # ct.show(tree_style=ts) filedir = '/'.join(filename.split('/')[:-1]) # t.write(format=9, outfile="output/newick/"+param+".nw") ct.render(filedir+'/phylo.png',tree_style=ts)
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') ts = TreeStyle() ts.layout_fn= ColorCodedNode ts.mode='c' #Circular tree ts.show_scale = False ts.show_leaf_name = False ts.draw_guiding_lines=True t.render("mytree50.png", w=3840, units="px",tree_style=ts)
def make_cluster_tree(tree_file: str, matrix: str, out_file: str, outgroup: Optional[List[str]] = None) -> None: """Draw a tree with cluster absence/presence information from an existing tree file and absence/presence matrix, and save it as an image under the supplied file name. Arguments: tree_file: the name of the file containing the tree to annotate matrix: a comma- or tab-separated absence/presence matrix out_file: the name under which to save the resulting image outgroup: the organism(s) to use as an outgroup, if any """ # ClusterTree needs tab-separated, but that can't be exported cleanly matrix = matrix.replace(",", "\t") # tree with clustering analysis tree = ClusterTree(tree_file, text_array=matrix) # rerooting the tree if outgroup: ancestor = tree.get_common_ancestor(outgroup) tree.set_outgroup(ancestor) tree.ladderize(direction=1) # set drawing line width to 2 my_node_style = NodeStyle() my_node_style["vt_line_width"] = 2 my_node_style["hz_line_width"] = 2 my_node_style["size"] = 5 # layout function def sel_mylayout(node): node.set_style(my_node_style) if node.is_leaf(): # add names in larger font + italics species_name = AttrFace("name", fsize=12, fstyle="italic") add_face_to_node(species_name, node, column=0, position="branch-right") # add absence/presence matrix for i, value in enumerate(getattr(node, "profile", [])): if value > 0: color = "#FF0000" else: color = "#EEEEEE" my_face = CircleFace(8, color, style="circle") my_face.margin_right = 3 my_face.margin_bottom = 3 add_face_to_node(my_face, node, position="aligned", column=i) # Use my layout to visualize the tree my_tree_style = TreeStyle() # Add header for j, name in enumerate(tree.arraytable.colNames): name_face = TextFace(name, fsize=11) name_face.rotation = -90 name_face.hz_align = 1 name_face.vt_align = 1 name_face.margin_bottom = 10 my_tree_style.aligned_header.add_face(name_face, column=j) my_tree_style.scale_length = 0.1 # myTreeStyle.show_branch_support = True # don't auto-show leaf names, since we dealt with that above my_tree_style.show_leaf_name = False # set layout function for my_tree_style my_tree_style.layout_fn = sel_mylayout #tree.render(out_file, w=183, units="mm", dpi=600, tree_style=my_tree_style) tree.render(out_file, dpi=600, tree_style=my_tree_style)
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) '''
f.close() t_str = nkTree[0] t = ClusterTree(t_str, data_mat) ts = TreeStyle() ts.margin_left = 20 ts.margin_right = 20 ts.margin_top = 20 ts.margin_bottom = 10 ts.scale = 2 ts.min_leaf_separation = 0 ts.branch_vertical_margin = 0 ts.show_leaf_name = True ts.show_branch_length = False ts.show_branch_support = True setup_heatmap(t, ts, header, center_value=4.5, color_up=0.9, color_down=0.56, color_center="white") t.render(file_name=in_path + "/heatmap.pdf", tree_style=ts)
newick += ';' t = Tree(newick, format=1) ct = ClusterTree(t.write(), text_array=matrix) addColors(ct) # nodes are linked to the array table array = ct.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 np.isfinite(i)] matrix_max = np.max(matrix_dist) matrix_min = np.min(matrix_dist) matrix_avg = (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",colorscheme=3) # 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"]=2 # Use my layout to visualize the tree ts = TreeStyle() ts.layout_fn = mylayout # ct.show(tree_style=ts) t.write(format=9, outfile="output/newick/"+param+".nw") ct.render('output/pngs/'+param+'.png',tree_style=ts) #print t