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) '''