Пример #1
0
def barchart_layout(node, name='name',
                    width=20, height=40,
                    colors=None, min_value=0, max_value=1,
                    fsize=14, fgcolor="black"):
    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=['#0000FF'], min_value=min_value,
                         max_value=max_value)
        # Let's make the sphere transparent
        C.opacity = 0.5
        # Rotate the faces by 270*
        C.rotation = 270
        # And place as a float face over the tree
        faces.add_face_to_node(C, node, 0, position="float")
Пример #2
0
        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")
Пример #3
0
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")
Пример #4
0
    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)
Пример #5
0
    def custom_layout(self,node):
        if node.is_leaf():
           aligned_name_face = TextFace(node.name, fgcolor='olive', fsize=12)
           aligned_name_face.margin_top = 5
           aligned_name_face.margin_right = 5
           aligned_name_face.margin_left = 5
           aligned_name_face.margin_bottom = 5
           aligned_name_face.hz_align = 0     #0 = left, 1 = center, 2 = right 
           add_face_to_node(aligned_name_face, node, column=2, position='aligned')
           #name_face = TextFace(node.name, fgcolor='#333333', fsize=11)
           #name_face.margin_top = 3
           #name_face.margin_right = 3
           #name_face.margin_left = 3
           #name_face.margin_bottom = 3 
           #add_face_to_node(name_face, node, column=2, position='branch-right')
           node.img_style['size'] = 0
           #---------------------------------------------
           #displaying extra categorical and numeric data
           if (node.name in self._tip2info):
              column_no = 3
              for headerIndex, dataheader in enumerate(self._tip2headers):
                  extra_data = self._tip2info[node.name][headerIndex]
                  if isinstance( extra_data, ( int, float ) ):
                     extra_face = BarChartFace([extra_data], width=100,height=25,colors=[self._tip2color[node.name][headerIndex]],labels=[dataheader],min_value=0.0,max_value=self._tip_max)
                  else:
                     extra_face = TextFace(extra_data, fsize=11, fgcolor='black')
                     extra_face.background.color = self._tip2color[node.name][headerIndex]

                  extra_face.margin_left = 5
                  extra_face.margin_top = 5
                  extra_face.margin_right = 5
                  extra_face.margin_bottom = 5
                   
                  add_face_to_node(extra_face, node, column=column_no, position='aligned')
                  #add_face_to_node(extra_face, node, column=column_no, aligned = True, position='branch-right')
                  column_no += 1
           else:
              #print "No data available"
              column_no = 3
              for headerIndex, dataheader in enumerate(self._tip2headers):     
                  extra_face = TextFace("No data available", fsize=10, fgcolor='black')
         
                  extra_face.margin_left = 5
                  extra_face.margin_top = 5
                  extra_face.margin_right = 5
                  extra_face.margin_bottom = 5
              
                  add_face_to_node(extra_face, node, column=column_no, position='aligned')
                  column_no += 1

           image_col_no = column_no
           #----------------------------------------------
           if (node.name in self._img_chk_list):
              if self._img_data_dic[node.name] is not None:
                  img_face = ImgFace(self._img_data_dic[node.name], is_url=True)
                  #img_face = ImgFace(self._tip2info[node.name][0], is_url=True)
                  #img_path = os.path.join("file:///home/tayeen/TayeenFolders/TreeViewer/WebTreeApp/newplugin_test/data/", "328653.jpg")
                  #img_face = ImgFace(img_path, is_url=True)
                  img_face.margin_top = 10
                  img_face.margin_right = 10
                  img_face.margin_left = 10
                  img_face.margin_bottom = 10
                  #add_face_to_node(img_face, node, column=3, position='branch-right')
                  #add_face_to_node(img_face, node, column=3, aligned= True, position='branch-right')
              else:
                  img_path = os.path.join("file://"+image_path, "ina.jpg")
                  img_face = ImgFace(img_path, is_url=True)  
              
              #add_face_to_node(img_face, node, column=5, position='branch-right')
              add_face_to_node(img_face, node, column=image_col_no, position='aligned')
                   
        else: #node is not a leaf
            node.img_style['size'] = 4
            node.img_style['shape'] = 'square'
        
            if node.name and self._custom_options["draw_internal"]:
              name_face = TextFace(node.name, fgcolor='grey', fsize=10)
              name_face.margin_top = 4
              name_face.margin_right = 4
              name_face.margin_left = 4
              name_face.margin_bottom = 4
              add_face_to_node(name_face, node, column=0, position='branch-top')
            
            if node.name in self._node2label: 
               label_face = TextFace(self._node2label[node.name], fgcolor='DarkGreen', fsize=10)
               label_face.margin_top = 4
               label_face.margin_right = 4
               label_face.margin_left = 4
               label_face.margin_bottom = 4
               add_face_to_node(label_face, node, column=0, position="branch-top")
            
            if node.support and self._custom_options["draw_support"]:
              support_face = TextFace(node.support, fgcolor='indianred', fsize=10)
              support_face.margin_top = 4
              support_face.margin_right = 4
              support_face.margin_left = 4
              support_face.margin_bottom = 4
              add_face_to_node(support_face, node, column=0, position='branch-bottom')
              
              

            if hasattr(node, "hide") and int(node.hide) == 1:
              node.img_style["draw_descendants"]= False
              collapsed_face = faces.TextFace(" %s collapsed leaves." %len(node), \
                    fsize=10, fgcolor="#444", ftype="Arial")
              faces.add_face_to_node(collapsed_face, node, 0)
            else:
              node.img_style["draw_descendants"] = True

            # Parse node features features and conver them into styles. This must be done like this, since current ete version 
            #does not allow modifying style outside the layout function.
            if hasattr(node, "bsize"):
              node.img_style["size"]= int(node.bsize)

            if hasattr(node, "shape"):
              node.img_style["shape"]= node.shape

            if hasattr(node, "bgcolor"):
              node.img_style["bgcolor"]= node.bgcolor

            if hasattr(node, "fgcolor"):
              node.img_style["fgcolor"]= node.fgcolor
        #parse all nodes features
        
        if hasattr(node, "bh_bgcolor"):
           node.img_style["bgcolor"]= node.bh_bgcolor
        if hasattr(node, "bh_size"):
           node.img_style["size"]= node.bh_size
       
        if hasattr(node, "lh_color"):
           node.img_style['hz_line_color'] = node.lh_color
           node.img_style["vt_line_color"] = node.lh_color
        
        if hasattr(node, "lh_width"):
           node.img_style['hz_line_width'] = node.lh_width
           node.img_style['vt_line_width'] = node.lh_width

        if hasattr(node, "lh_width") and hasattr(node, "lh_color"):
           for n in node.iter_descendants():
               n.img_style['hz_line_color'] = node.lh_color
               n.img_style["vt_line_color"] = node.lh_color
               n.img_style['hz_line_width'] = node.lh_width
               n.img_style['vt_line_width'] = node.lh_width