def _layout(node): if self.hog is None: _label = [str(node.nbr_genes),str(node.retained),str(node.dupl),str(node.gain),str(node.lost)] else: _label = [str(node.nbr_genes), str(node.retained), str(node.dupl), str(node.lost)] def _add_face(name_feature, value_feature, cnum=1, pos="branch-right"): node.add_face(TextFace("{}: {}".format(name_feature, value_feature)), column=cnum, position=pos) def _add_faces(cNbr=1, posNbr="branch-right", cAttr=2, posAtt="branch-right"): _add_face("#genes", node.nbr_genes, cnum=cNbr, pos=posNbr) if node.retained is not None: _add_face("#Retained", node.retained, cnum=cAttr, pos=posAtt) if node.dupl is not None: _add_face("#Duplicated", node.dupl, cnum=cAttr, pos=posAtt) if node.gain is not None: _add_face("#Novel", node.gain, cnum=cAttr, pos=posAtt) if node.lost is not None: _add_face("#Lost", node.lost, cnum=cAttr, pos=posAtt) if node.is_leaf(): if display_internal_histogram: if self.hog is None: values = [node.nbr_genes,node.retained,node.dupl,node.gain,node.lost] w_plot = 50 else: values = [node.nbr_genes, node.retained, node.dupl, node.lost] w_plot = 40 node.add_face(BarChartFace(values, deviations=None, width=w_plot, height=25, colors=_color_scheme, labels=_label, min_value=0, max_value=max_genes, label_fsize=6, scale_fsize=6),column=1, position = "branch-right") else: _add_faces() else: if display_internal_histogram: if node.is_root(): node.add_face(BarChartFace([node.nbr_genes], deviations=None, width=10, height=25, colors=["#41c1c2"], labels=[str(node.nbr_genes)], min_value=0, max_value=max_genes, label_fsize=6, scale_fsize=6),column=0, position = "branch-bottom") else: if self.hog is None: values = [node.nbr_genes,node.retained,node.dupl,node.gain,node.lost] w_plot = 50 else: values = [node.nbr_genes,node.retained,node.dupl,node.lost] w_plot = 40 node.add_face(BarChartFace(values, deviations=None, width=w_plot, height=25, colors=_color_scheme, labels=_label, min_value=0, max_value=max_genes, label_fsize=6, scale_fsize=6),column=1, position = "branch-top") else: _add_faces(cNbr=0, posNbr="branch-top", cAttr=0, posAtt="branch-bottom")
def barchart_layout(node, name='name', width=20, height=40, colors=None, min_value=0, max_value=1, fsize=14, fgcolor="black", alpha=0.5, rotation=270): """ Specifies the layout for the ete.TreeStyle object. Parameters ---------- node: ete.Tree Input node for specifying which attributes. name: str, optional Attribute to look up the name of the node. width: int, optional Width of the barchart. height: int, optional Height of the barchart. colors: list of str, optional List of HTML colors to color the barchart values. min_value: int, optional Minimum value to set the scale of the chart. max_value: int, optional Maximum value to set the scale of the chart. fsize: int, optional Font size on the leafs. fgcolor: str, optional Font color of the leafs. alpha: float, optional Transparency of the barchart. rotation: int, optional Orientation of the barchart. """ if colors is None: colors = ['#0000FF'] if node.is_leaf(): # Add node name to leaf nodes N = AttrFace("name", fsize=fsize, fgcolor=fgcolor) faces.add_face_to_node(N, node, 0) if "weight" in node.features: # Creates a sphere face whose size is proportional to node's # feature "weight" if (isinstance(node.weight, int) or isinstance(node.weight, float)): weight = [node.weight] else: weight = node.weight C = BarChartFace(values=weight, width=width, height=height, colors=colors, min_value=min_value, max_value=max_value) # Let's make the sphere transparent C.opacity = alpha # Rotate the faces by 270* C.rotation = rotation # And place as a float face over the tree faces.add_face_to_node(C, node, 0, position="float")
def export(self, output, layout_function=None, display_internal_histogram=True): """ Method to export the tree profile object as figure (available format .SVG, .PDF, .PNG). -- Some magic going there -- Args: | output (:obj:`str`): output file name. The extension of output will set the format of the figure (SVG, .PDF, .PNG) | layout_function (:obj:`function`, optional): custom layout_fn for ete3 TreeStyle. | display_internal_histogram (:obj:`Boolean`, optional): Display internal node as histogram or raw text with numbers. Defaults to True. """ from ete3 import TreeStyle, TextFace, NodeStyle, BarChartFace # maximum number of genes per node in this treeMap max_genes = max([d for d in self.treemap.traverse()], key=lambda x:x.nbr_genes).nbr_genes if self.hog is None: _color_scheme = ["#41c1c2","#bdc3c7","#f39c12","#27ae60","#e74c3c"] _label_legend = ["Genes","Retained","Duplicated","Novel","Lost"] _values_legend = [max_genes,max_genes,max_genes,max_genes,max_genes] w_legend = 50 # todo calculate base on len(_values) else: _color_scheme = ["#41c1c2", "#bdc3c7", "#f39c12", "#e74c3c"] _label_legend = ["Genes", "Retained", "Duplicated", "Lost"] _values_legend = [max_genes, max_genes, max_genes, max_genes] w_legend = 40 # todo calculate base on len(_values) def _layout(node): if self.hog is None: _label = [str(node.nbr_genes),str(node.retained),str(node.dupl),str(node.gain),str(node.lost)] else: _label = [str(node.nbr_genes), str(node.retained), str(node.dupl), str(node.lost)] def _add_face(name_feature, value_feature, cnum=1, pos="branch-right"): node.add_face(TextFace("{}: {}".format(name_feature, value_feature)), column=cnum, position=pos) def _add_faces(cNbr=1, posNbr="branch-right", cAttr=2, posAtt="branch-right"): _add_face("#genes", node.nbr_genes, cnum=cNbr, pos=posNbr) if node.retained is not None: _add_face("#Retained", node.retained, cnum=cAttr, pos=posAtt) if node.dupl is not None: _add_face("#Duplicated", node.dupl, cnum=cAttr, pos=posAtt) if node.gain is not None: _add_face("#Novel", node.gain, cnum=cAttr, pos=posAtt) if node.lost is not None: _add_face("#Lost", node.lost, cnum=cAttr, pos=posAtt) if node.is_leaf(): if display_internal_histogram: if self.hog is None: values = [node.nbr_genes,node.retained,node.dupl,node.gain,node.lost] w_plot = 50 else: values = [node.nbr_genes, node.retained, node.dupl, node.lost] w_plot = 40 node.add_face(BarChartFace(values, deviations=None, width=w_plot, height=25, colors=_color_scheme, labels=_label, min_value=0, max_value=max_genes, label_fsize=6, scale_fsize=6),column=1, position = "branch-right") else: _add_faces() else: if display_internal_histogram: if node.is_root(): node.add_face(BarChartFace([node.nbr_genes], deviations=None, width=10, height=25, colors=["#41c1c2"], labels=[str(node.nbr_genes)], min_value=0, max_value=max_genes, label_fsize=6, scale_fsize=6),column=0, position = "branch-bottom") else: if self.hog is None: values = [node.nbr_genes,node.retained,node.dupl,node.gain,node.lost] w_plot = 50 else: values = [node.nbr_genes,node.retained,node.dupl,node.lost] w_plot = 40 node.add_face(BarChartFace(values, deviations=None, width=w_plot, height=25, colors=_color_scheme, labels=_label, min_value=0, max_value=max_genes, label_fsize=6, scale_fsize=6),column=1, position = "branch-top") else: _add_faces(cNbr=0, posNbr="branch-top", cAttr=0, posAtt="branch-bottom") ts = TreeStyle() if layout_function is not None: ts.layout_fn = layout_function else: ts.layout_fn = _layout ts.legend.add_face(BarChartFace(_values_legend, deviations=None, width=w_legend, height=25, colors=_color_scheme, labels=_label_legend, min_value=0, max_value=max_genes, label_fsize=6, scale_fsize=6),column=0) ts.legend_position = 3 self.treemap.render(output,tree_style=ts)