def custom_layout(node):
    if node.is_leaf():
        aligned_name_face = TextFace(node.name, fgcolor='olive', fsize=14)
        add_face_to_node(aligned_name_face, node, column=2, position='aligned')
        name_face = TextFace(node.name, fgcolor='#333333', fsize=11)
        add_face_to_node(name_face, node, column=2, position='branch-right')
        node.img_style['size'] = 0

        if (node.name in tip2info) and (node.name in image_checker):
            # image
            img_face = ImgFace(tip2info[node.name][0], is_url=True)
            add_face_to_node(img_face, node, column=4, position='branch-right')

            habitat_face = TextFace(tip2info[node.name][2], fsize=11, fgcolor='white')
            habitat_face.background.color = 'steelblue'
            habitat_face.margin_left = 3
            habitat_face.margin_top = 3
            habitat_face.margin_right = 3
            habitat_face.margin_bottom = 3
            add_face_to_node(habitat_face, node, column=3, position='aligned')
    else:
        node.img_style['size'] = 4
        node.img_style['shape'] = 'square'
        if node.name:
            name_face = TextFace(node.name, fgcolor='grey', fsize=10)
            name_face.margin_bottom = 2
            add_face_to_node(name_face, node, column=0, position='branch-top')
        if node.support:
            support_face = TextFace(node.support, fgcolor='indianred', fsize=10)
            add_face_to_node(support_face, node, column=0, position='branch-bottom')
Ejemplo n.º 2
0
 def add_text_face(self,
                   taxon2text,
                   header_name,
                   color_scale=False):
     
     from metagenlab_libs.colors import get_categorical_color_scale
     
     if color_scale:
         value2color = get_categorical_color_scale(taxon2text.values())
     
     self._add_header(header_name)
    
     # add column
     for i, lf in enumerate(self.tree.iter_leaves()):
         if lf.name in taxon2text:
             n = TextFace('%s' % taxon2text[lf.name])
             if color_scale:
                 n.background.color = value2color[taxon2text[lf.name]]
         else:
             print(lf.name, "not in", taxon2text)
             n = TextFace('-')
         n.margin_top = 1
         n.margin_right = 10
         n.margin_left = 10
         n.margin_bottom = 1
         n.opacity = 1.
         if self.rotate:
             n.rotation= 270
         lf.add_face(n, self.column_count, position="aligned")
         
     self.column_count += 1
def draw_tree(tree):
    tree_copy = tree.copy("newick-extended")
    tree_copy.add_feature("i", tree.i)
    tree_copy.add_feature("Condition", tree.Condition)

    for n in tree_copy.traverse():
        if n.is_leaf():
                n.set_style(nstyle)
                n.add_face(TextFace(str(n.name)), column=0, position="aligned")
        else:
            n.set_style(nstyle)
        nd = TextFace(str(n.i))
        nd.background.color = condi_color_dic[str(n.Condition)]
        nd.margin_right = 2
        nd.margin_top = 1
        nd.margin_left = 2
        nd.margin_bottom = 1
        nd.border.width = 1
        if add_transition:
            if hasattr(n, "Transition"):
                nd.border.color = "red"
                nd.border.width = 2
        n.add_face(nd, column=0, position="float")
        n.add_face(TextFace("       "), column=0, position="branch-bottom")
    return tree_copy
Ejemplo n.º 4
0
def custom_layout(node):
    if node.is_leaf():
        aligned_name_face = TextFace(node.name, fgcolor='olive', fsize=14)
        add_face_to_node(aligned_name_face, node, column=2, position='aligned')
        name_face = TextFace(node.name, fgcolor='#333333', fsize=11)
        add_face_to_node(name_face, node, column=2, position='branch-right')
        node.img_style['size'] = 0

        if node.name in tip2info:
            # For some reason img urls are very slow!
            #img_face = ImgFace(tip2info[node.name][0], is_url=True)
            #add_face_to_node(img_face, node, column=4, position='branch-right')
            habitat_face = TextFace(tip2info[node.name][2], fsize=11, fgcolor='white')
            habitat_face.background.color = 'steelblue'
            habitat_face.margin_left = 3
            habitat_face.margin_top = 3
            habitat_face.margin_right = 3
            habitat_face.margin_bottom = 3
            add_face_to_node(habitat_face, node, column=3, position='aligned')
    else:
        node.img_style['size'] = 4
        node.img_style['shape'] = 'square'
        if node.name:
            name_face = TextFace(node.name, fgcolor='grey', fsize=10)
            name_face.margin_bottom = 2
            add_face_to_node(name_face, node, column=0, position='branch-top')
        if node.support:
            support_face = TextFace(node.support, fgcolor='indianred', fsize=10)
            add_face_to_node(support_face, node, column=0, position='branch-bottom')
Ejemplo n.º 5
0
def styleFace(val):
    x = TextFace(val)
    x.margin_bottom = 5
    x.margin_right  = 10
    x.rotation      = 270
    x.fsize         = 6
    return x
Ejemplo n.º 6
0
def traitTree(traits, mapper, outDir):
    ### Take dict of traits and [R,G,B]-returning function
    ### Draw a tree with the continuous trait painted on via a colormapping function

    def rgb2hex(r, g, b):
        hex = "#{:02x}{:02x}{:02x}".format(r, g, b)
        return hex

    for n in tree.traverse():
        if n.is_leaf():
            n.set_style(nstyle_L)
            n.add_face(TextFace(str(n.name)), column=0, position="aligned")
        else:
            n.set_style(nstyle)
        #nd = TextFace(str(n.ND)) # label with node ID
        nd = TextFace(str(int(
            traits[n.ND])))  # label with rounded continuous trait value

        nd.background.color = rgb2hex(*[
            int(val) for val in mapper(traits[n.ND], gamma=0.8, scaleMax=255)
        ])  # setup for wl2RGB
        nd.margin_right = 2
        nd.margin_top = 1
        nd.margin_left = 2
        nd.margin_bottom = 1
        nd.border.width = 1
        n.add_face(nd, column=0, position="float")
        n.add_face(TextFace("       "), column=0, position="branch-bottom")

    #outFile = args.output + "/test_trees/cont_trait.pdf"
    outFile = outDir + "/cont_trait.pdf"
    tree.render(outFile, tree_style=tree_style)
    print >> sys.stderr, outFile
Ejemplo n.º 7
0
 def my_layout(node):
      F = TextFace(node.name, tight_text=True)
      F.fsize=6
      F.margin_left=5
      F.margin_right=5
      F.margin_top=0
      F.margin_bottom=15
      F.rotation=-90
      add_face_to_node(F, node, column=0, position="branch-right")
Ejemplo n.º 8
0
    def style_and_render_tree(self, file_types=["svg"]):
        from ete3 import TreeStyle, TextFace, CircleFace

        ts = TreeStyle()
        title_face = TextFace(f"{genus} {snakemake.config['species']}", fsize=20)
        title_face.margin_bottom = 10
        ts.title.add_face(title_face, column=0)
        ts.branch_vertical_margin = 10
        ts.show_leaf_name = True
        # Legend
        ts.legend.add_face(TextFace(""), column=1)
        for category in ["Allowed", "Deviations", "Filtered", "Color"]:
            category = TextFace(category, fsize=8, bold=True)
            category.margin_bottom = 2
            category.margin_right = 40
            ts.legend.add_face(category, column=1)
        for i, criteria in enumerate(CRITERIA, 2):
            title = criteria.replace("_", " ").title()
            title = TextFace(title, fsize=8, bold=True)
            title.margin_bottom = 2
            title.margin_right = 40
            cf = CircleFace(4, COLORS[criteria], style="sphere")
            cf.margin_bottom = 5
            filtered_count = len(
                list(filter(None, self.failed_report.criteria == criteria))
            )
            filtered = TextFace(filtered_count, fsize=8)
            filtered.margin_bottom = 5
            allowed = TextFace(self.allowed[criteria], fsize=8)
            allowed.margin_bottom = 5
            allowed.margin_right = 25
            # TODO Prevent tolerance from rendering as a float
            tolerance = TextFace(self.tolerance[criteria], fsize=8)
            tolerance.margin_bottom = 5
            ts.legend.add_face(title, column=i)
            ts.legend.add_face(allowed, column=i)
            ts.legend.add_face(tolerance, column=i)
            ts.legend.add_face(filtered, column=i)
            ts.legend.add_face(cf, column=i)
        for f in file_types:
            out_tree = os.path.join(self.paths.qc, "tree.{}".format(f))
            self.tree.render(out_tree, tree_style=ts)
Ejemplo n.º 9
0
    def draw(self,
             file,
             colors,
             color_internal_nodes=True,
             legend_labels=(),
             show_branch_support=True,
             show_scale=True,
             legend_scale=1,
             mode="c",
             neighbours=None,
             neighbours_block=None):
        max_color = len(colors)

        used_colors = set()
        for node in self.tree.traverse():
            if not (color_internal_nodes or node.is_leaf()): continue
            color = colors[min(node.color, max_color - 1)]
            node.img_style['bgcolor'] = color
            used_colors.add(color)

        ts = TreeStyle()
        ts.mode = mode
        ts.scale = self.scale
        # Disable the default tip names config
        ts.show_leaf_name = False
        ts.show_branch_support = show_branch_support

        # ts.branch_vertical_margin = 20
        ts.show_scale = show_scale
        cur_max_color = max(v.color for v in self.tree.traverse())
        current_colors = colors[0:cur_max_color + 1]

        for i, (label, color_) in enumerate(zip(legend_labels,
                                                current_colors)):
            if color_ not in used_colors: continue
            rf = RectFace(20 * legend_scale, 16 * legend_scale, color_, color_)
            rf.inner_border.width = 1
            rf.margin_right = 14
            rf.margin_left = 14

            tf = TextFace(label, fsize=26 * legend_scale)
            tf.margin_right = 14

            ts.legend.add_face(rf, column=0)
            ts.legend.add_face(tf, column=1)

        if neighbours:
            old_tree = self.tree.copy()
            self.draw_neighbours(neighbours, neighbours_block)

        self.tree.render(file, w=1000, tree_style=ts)

        if neighbours:
            self.tree = old_tree
Ejemplo n.º 10
0
    def add_node(self, node_text, edge_text, is_max):
        new = self.curr_node.add_child()
        new.add_face(TextFace(node_text, fsize=12, fgcolor='darkred'),
                     column=0,
                     position='branch-right')

        tf = TextFace(edge_text, fsize=9, fgcolor='darkgoldenrod')
        tf.margin_right = 30
        new.add_face(tf, column=0, position='branch-bottom')
        new.set_style(NodeStyle(size=14,
                                fgcolor=('blue' if is_max else 'red')))
        self.curr_node = new
Ejemplo n.º 11
0
 def style_and_render_tree(self, file_types=["svg"]):
     from ete3 import TreeStyle, TextFace, CircleFace
     ts = TreeStyle()
     title_face = TextFace(self.name.replace('_', ' '), fsize=20)
     title_face.margin_bottom = 10
     ts.title.add_face(title_face, column=0)
     ts.branch_vertical_margin = 10
     ts.show_leaf_name = False
     # Legend
     ts.legend.add_face(TextFace(""), column=1)
     for category in ["Allowed", "Tolerance", "Filtered", "Color"]:
         category = TextFace(category, fsize=8, bold=True)
         category.margin_bottom = 2
         category.margin_right = 40
         ts.legend.add_face(category, column=1)
     for i, criteria in enumerate(self.criteria, 2):
         title = criteria.replace("_", " ").title()
         title = TextFace(title, fsize=8, bold=True)
         title.margin_bottom = 2
         title.margin_right = 40
         cf = CircleFace(4, self.colors[criteria], style="sphere")
         cf.margin_bottom = 5
         filtered_count = len(
             list(filter(None, self.failed_report.criteria == criteria)))
         filtered = TextFace(filtered_count, fsize=8)
         filtered.margin_bottom = 5
         allowed = TextFace(self.allowed[criteria], fsize=8)
         allowed.margin_bottom = 5
         allowed.margin_right = 25
         tolerance = TextFace(self.tolerance[criteria], fsize=8)
         tolerance.margin_bottom = 5
         ts.legend.add_face(title, column=i)
         ts.legend.add_face(allowed, column=i)
         ts.legend.add_face(tolerance, column=i)
         ts.legend.add_face(filtered, column=i)
         ts.legend.add_face(cf, column=i)
     for f in file_types:
         out_tree = os.path.join(self.qc_results_dir, 'tree.{}'.format(f))
         self.tree.render(out_tree, tree_style=ts)
         self.log.info("tree.{} generated".format(f))
Ejemplo n.º 12
0
def testTrees(scenarios, outDir):

    ### Draw test trees. This is a modified version of the test routine in pcoc_num_tree.py, stuffed in a for loop
    for cutoff in sorted(scenarios.keys()):
        tree = init_tree(args.tree)
        # not mucking with additive trees yet; ultrametrize the tree and normalize to length 1
        tree.convert_to_ultrametric(tree_length=1)
        manual_mode_nodes = {}
        manual_mode_nodes = {"T": [], "C": []}
        p_events = scenarios[cutoff].strip().split("/")
        for e in p_events:
            l_e = map(int, e.split(","))
            manual_mode_nodes["T"].append(l_e[0])
            manual_mode_nodes["C"].extend(l_e[1:])

        for n in tree.traverse():
            if n.is_leaf():
                n.set_style(nstyle_L)
                n.add_face(TextFace(str(n.name)), column=0, position="aligned")
            else:
                n.set_style(nstyle)
            nd = TextFace(str(n.ND))

            if manual_mode_nodes:
                if n.ND in manual_mode_nodes["T"]:
                    nd.background.color = "red"
                elif n.ND in manual_mode_nodes["C"]:
                    nd.background.color = "orange"
                else:
                    nd.background.color = "white"
            else:
                nd.background.color = "white"
                nd.background.color = "white"
            nd.margin_right = 2
            nd.margin_top = 1
            nd.margin_left = 2
            nd.margin_bottom = 1
            nd.border.width = 1
            n.add_face(nd, column=0, position="float")
            n.add_face(TextFace("       "), column=0, position="branch-bottom")

        # if --float set, limit number of digits in filename
        if args.float:
            outFile = str(cutoff).replace(
                '.', '_')[:np.min([args.float, len(str(cutoff))])] + ".pdf"
        else:
            outFile = str(cutoff).replace('.', '_') + ".pdf"

        # prepend path to filename
        outFile = outDir + '/' + outFile
        tree.render(outFile, tree_style=tree_style)
        print >> sys.stderr, outFile
Ejemplo n.º 13
0
def add_t(node):
    nd = TextFace("-")
    nd.fsize = 4
    nd.background.color = "black"
    nd.margin_right = 0
    nd.margin_top = 0
    nd.margin_left = 0
    nd.margin_bottom = 0
    nd.border.width = 1
    nd2 = TextFace(" ")
    nd2.fsize = 4

    node.add_face(nd, column=0, position = "float")
    node.add_face(nd2, column=1, position = "float")
Ejemplo n.º 14
0
    def add_continuous_colorscale_legend(self,
                                         title,
                                         min_val, 
                                         max_val,
                                         scale):
        
        self.tss.legend.add_face(TextFace(f"{title}", fsize = 4 * self.text_scale), column=0)
        
        if min_val != max_val:
            n = TextFace(" " * int(self.text_scale), fsize = 4 * self.text_scale)
            n.margin_top = 1
            n.margin_right = 1
            n.margin_left = 10
            n.margin_bottom = 1
            n.inner_background.color = rgb2hex(scale[0].to_rgba(float(max_val)))
            
            n2 = TextFace(" " * int(self.text_scale), fsize = 4 * self.text_scale)
            n2.margin_top = 1
            n2.margin_right = 1
            n2.margin_left = 10
            n2.margin_bottom = 1
            n2.inner_background.color = rgb2hex(scale[0].to_rgba(float(min_val)))

            self.tss.legend.add_face(n, column=1)
            self.tss.legend.add_face(TextFace(f"{max_val} % (max)", fsize = 4 * self.text_scale), column=2)
            self.tss.legend.add_face(n2, column=1)
            self.tss.legend.add_face(TextFace(f"{min_val} % (min)", fsize = 4 * self.text_scale), column=2)   
        else:
            n2 = TextFace(" " * int(self.text_scale), fsize = 4 * self.text_scale)
            n2.margin_top = 1
            n2.margin_right = 1
            n2.margin_left = 10
            n2.margin_bottom = 1
            n2.inner_background.color = rgb2hex(scale[0].to_rgba(float(min_val)))

            self.tss.legend.add_face(n2, column=0)
            self.tss.legend.add_face(TextFace(f"{max_val} % Id", fsize = 4 * self.text_scale), column=1) 
Ejemplo n.º 15
0
 def _add_header(self, 
                header_name,
                column_add=0):
     
     n = TextFace(f'{header_name}')
     n.margin_top = 1
     n.margin_right = 1
     n.margin_left = 20
     n.margin_bottom = 1
     n.hz_align = 2
     n.vt_align = 2
     n.rotation = 270
     n.inner_background.color = "white"
     n.opacity = 1.
     # add header
     self.tss.aligned_header.add_face(n, self.column_count-1+column_add)
Ejemplo n.º 16
0
    def ly_tax_labels(node):
        if node.is_leaf():
            c = LABEL_START_COL
            largest = 0
            for tname in TRACKED_CLADES:
                if hasattr(node, "named_lineage") and tname in node.named_lineage:
                    linF = TextFace(tname, fsize=10, fgcolor='white')
                    linF.margin_left = 3
                    linF.margin_right = 2
                    linF.background.color = lin2color[tname]
                    add_face_to_node(linF, node, c, position='aligned')
                    c += 1

            for n in range(c, len(TRACKED_CLADES)):
                add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'), node, c, position='aligned')
                c+=1
Ejemplo n.º 17
0
    def add_heatmap(self, 
                    taxon2value, 
                    header_name,
                    continuous_scale=False,
                    show_text=False):
        
        from metagenlab_libs.colors import get_continuous_scale
        
        self._add_header(header_name)
                
        if continuous_scale:
            color_scale = get_continuous_scale(taxon2value.values())
        
        for i, lf in enumerate(self.tree.iter_leaves()):
            
            if not lf.name in taxon2value:
                n = TextFace('')
            else:
                value = taxon2value[lf.name]

                if show_text:
                    n = TextFace('%s' % value)
                else:
                    n = TextFace('    ')

                n.margin_top = 2
                n.margin_right = 3
                n.margin_left = 3
                n.margin_bottom = 2
                n.hz_align = 1
                n.vt_align = 1
                n.border.width = 3
                n.border.color = "#ffffff"
                if continuous_scale:
                    n.background.color = rgb2hex(color_scale[0].to_rgba(float(value)))
                n.opacity = 1.
                i+=1

            if self.rotate:
                n.rotation = 270
            lf.add_face(n, self.column_count, position="aligned")
        
        self.column_count += 1
Ejemplo n.º 18
0
def testTrees(scenarios, tree_style):
    ### Draw test trees. This is a modified version of the test routine in pcoc_num_tree.py, stuffed in a for loop
    for cutoff in sorted(scenarios.keys()):
        # this keeps attributes from stacking up in the same tree
        tree = init_tree(args.tree)
        manual_mode_nodes = {}
        manual_mode_nodes = {"T": [], "C": []}
        p_events = scenarios[cutoff].strip().split("/")
        for e in p_events:
            l_e = map(int, e.split(","))
            manual_mode_nodes["T"].append(l_e[0])
            manual_mode_nodes["C"].extend(l_e[1:])

        for n in tree.traverse():
            if n.is_leaf():
                n.set_style(nstyle_L)
                n.add_face(TextFace(str(n.name)), column=0, position="aligned")
            else:
                n.set_style(nstyle)
            nd = TextFace(str(n.ND))

            if manual_mode_nodes:
                if n.ND in manual_mode_nodes["T"]:
                    nd.background.color = "red"
                elif n.ND in manual_mode_nodes["C"]:
                    nd.background.color = "orange"
                else:
                    nd.background.color = "white"
            else:
                nd.background.color = "white"
                nd.background.color = "white"
            nd.margin_right = 2
            nd.margin_top = 1
            nd.margin_left = 2
            nd.margin_bottom = 1
            nd.border.width = 1
            n.add_face(nd, column=0, position="float")
            n.add_face(TextFace("       "), column=0, position="branch-bottom")

        outfile = args.output + "/test_trees/" + str(cutoff).replace('.','_') + ".pdf"
        tree.render(outfile, tree_style=tree_style)
        print >> sys.stderr, outfile
Ejemplo n.º 19
0
    def add_heatmap(self,
                    taxon2value, 
                    header_name,
                    scale_type="continuous",
                    palette=False):
        
        from metagenlab_libs.colors import get_categorical_color_scale
        from metagenlab_libs.colors import get_continuous_scale
        
        if scale_type == "continuous":
            scale = get_continuous_scale(taxon2value.values())
            self.add_continuous_colorscale_legend("Closest hit identity", 
                                                  min(taxon2value.values()),
                                                  max(taxon2value.values()), 
                                                  scale)
        elif scale_type == "categorical":
            scale = get_categorical_color_scale(taxon2value.values())
            self.add_categorical_colorscale_legend("MLST",
                                                   scale)
        else:
            raise IOError("unknown type")
        
        for i, lf in enumerate(self.tree.iter_leaves()):
            n = TextFace("   " * int(self.text_scale))
            if lf.name in taxon2value:
                value = taxon2value[lf.name]
                n = TextFace("   " * int(self.text_scale))
                if scale_type == "categorical":
                    n.inner_background.color = scale[value]
                if scale_type == "continuous":
                    n.inner_background.color = rgb2hex(scale[0].to_rgba(float(value)))

            n.margin_top = 0
            n.margin_right = 0
            n.margin_left = 10
            n.margin_bottom = 0
            n.opacity = 1.
            if self.rotate:
                n.rotation= 270
            lf.add_face(n, self.column_count, position="aligned")
            
        self.column_count += 1
Ejemplo n.º 20
0
    def ly_tax_labels(node):
        if node.is_leaf():
            c = LABEL_START_COL
            largest = 0
            for tname in TRACKED_CLADES:
                if hasattr(node,
                           "named_lineage") and tname in node.named_lineage:
                    linF = TextFace(tname, fsize=10, fgcolor='white')
                    linF.margin_left = 3
                    linF.margin_right = 2
                    linF.background.color = lin2color[tname]
                    add_face_to_node(linF, node, c, position='aligned')
                    c += 1

            for n in range(c, len(TRACKED_CLADES)):
                add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'),
                                 node,
                                 c,
                                 position='aligned')
                c += 1
Ejemplo n.º 21
0
    def add_categorical_colorscale_legend(self,
                                          title,
                                          scale):
        
        self.tss.legend.add_face(TextFace(f"{title}", fsize = 4 * self.text_scale), column=0)
        
        col = 1
        for n,value in enumerate(scale): 
            
            n2 = TextFace(" " * int(self.text_scale), fsize = 4 * self.text_scale)
            n2.margin_top = 1
            n2.margin_right = 1
            n2.margin_left = 10
            n2.margin_bottom = 1
            n2.inner_background.color = scale[value]

            self.tss.legend.add_face(n2, column=col)
            self.tss.legend.add_face(TextFace(f"{value}", fsize = 4 * self.text_scale), column=col+1)
            
            col+=2
            if col>16:
                self.tss.legend.add_face(TextFace(f"    ", fsize = 4 * self.text_scale), column=0)
                col = 1
Ejemplo n.º 22
0
def plot_tree_barplot(tree_file, taxon2mlst, header_list):
    '''

    display one or more barplot

    :param tree_file:
    :param taxon2value_list:
    :param exclude_outgroup:
    :param bw_scale:
    :param barplot2percentage: list of bool to indicates if the number are percentages and the range should be set to 0-100

    :return:
    '''

    import matplotlib.cm as cm
    from matplotlib.colors import rgb2hex
    import matplotlib as mpl

    mlst_list = list(set(taxon2mlst.values()))
    mlst2color = dict(zip(mlst_list, get_spaced_colors(len(mlst_list))))
    mlst2color['-'] = 'white'

    if isinstance(tree_file, Tree):
        t1 = tree_file
    else:
        t1 = Tree(tree_file)

    # Calculate the midpoint node
    R = t1.get_midpoint_outgroup()
    # and set it as tree outgroup
    t1.set_outgroup(R)

    tss = TreeStyle()
    value = 1
    tss.draw_guiding_lines = True
    tss.guiding_lines_color = "gray"
    tss.show_leaf_name = False

    cmap = cm.YlGnBu  #YlOrRd#OrRd

    scale_list = []
    max_value_list = []

    for i, lf in enumerate(t1.iter_leaves()):

        #if taxon2description[lf.name] == 'Pirellula staleyi DSM 6068':
        #    lf.name = 'Pirellula staleyi DSM 6068'
        #    continue
        if i == 0:
            # header

            col_add = 0

            #lf.add_face(n, column, position="aligned")
            n = TextFace('MLST')
            n.margin_top = 1
            n.margin_right = 2
            n.margin_left = 2
            n.margin_bottom = 1
            n.rotation = 90
            n.inner_background.color = "white"
            n.opacity = 1.
            n.hz_align = 2
            n.vt_align = 2

            tss.aligned_header.add_face(n, col_add + 1)

        try:
            #if lf.name in leaf2mlst or int(lf.name) in leaf2mlst:
            n = TextFace(' %s ' % taxon2mlst[int(lf.name)])
            n.inner_background.color = 'white'
            m = TextFace('  ')
            m.inner_background.color = mlst2color[taxon2mlst[int(lf.name)]]
        except:
            n = TextFace(' na ')
            n.inner_background.color = "grey"
            m = TextFace('    ')
            m.inner_background.color = "white"

        n.opacity = 1.
        n.margin_top = 2
        n.margin_right = 2
        n.margin_left = 0
        n.margin_bottom = 2

        m.margin_top = 2
        m.margin_right = 0
        m.margin_left = 2
        m.margin_bottom = 2

        lf.add_face(m, 0, position="aligned")
        lf.add_face(n, 1, position="aligned")

        n = TextFace(lf.name, fgcolor="black", fsize=12, fstyle='italic')
        lf.add_face(n, 0)

    for n in t1.traverse():
        nstyle = NodeStyle()
        if n.support < 1:
            nstyle["fgcolor"] = "black"
            nstyle["size"] = 6
            n.set_style(nstyle)
        else:
            nstyle["fgcolor"] = "red"
            nstyle["size"] = 0
            n.set_style(nstyle)

    return t1, tss
Ejemplo n.º 23
0
def plot_ete_tree(tree_file,
                  ordered_queries,
                  leaf_id2protein_id2identity,
                  leaf_id2mlst,
                  leaf_id2spa,
                  leaf_id2meca,
                  show_identity_values=True,
                  leaf_id2description=False):
    mlst_list = list(set(leaf_id2mlst.values()))
    mlst2color = dict(zip(mlst_list, get_spaced_colors(len(mlst_list))))
    mlst2color['-'] = 'white'

    t1 = Tree(tree_file)
    tss = TreeStyle()
    R = t1.get_midpoint_outgroup()
    t1.set_outgroup(R)
    t1.ladderize()

    head = True
    column_add = 4
    for lf in t1.iter_leaves():
        lf.branch_vertical_margin = 0
        # add MLST
        if head:
            n = TextFace(' MLST ')
            n.margin_top = 2
            n.margin_right = 2
            n.margin_left = 2
            n.margin_bottom = 2
            n.rotation = 270
            n.vt_align = 2
            n.hz_align = 2
            n.inner_background.color = "white"
            n.opacity = 1.
            tss.aligned_header.add_face(n, 1)

        if lf.name in leaf2mlst:
            n = TextFace(' %s ' % leaf_id2mlst[lf.name])
            n.inner_background.color = 'white'
            m = TextFace('  ')
            m.inner_background.color = mlst2color[leaf_id2mlst[lf.name]]
        else:
            n = TextFace(' na ')
            n.inner_background.color = "grey"
            m = TextFace('    ')
            m.inner_background.color = "white"

        n.opacity = 1.
        n.margin_top = 2
        n.margin_right = 2
        n.margin_left = 0
        n.margin_bottom = 2

        m.margin_top = 2
        m.margin_right = 0
        m.margin_left = 20
        m.margin_bottom = 2

        lf.add_face(m, 0, position="aligned")
        lf.add_face(n, 1, position="aligned")

        # add spa typing
        if head:
            n = TextFace(' spa ')
            n.margin_top = 2
            n.margin_right = 2
            n.margin_left = 2
            n.margin_bottom = 2
            n.rotation = 270
            n.vt_align = 2
            n.hz_align = 2
            n.inner_background.color = "white"
            n.opacity = 1.
            tss.aligned_header.add_face(n, column_add-2)
        if lf.name in leaf_id2spa:
            n = TextFace(' %s ' % leaf_id2spa[lf.name])
            n.inner_background.color = "white"
        else:
            n = TextFace('  na  ')
            n.inner_background.color = "grey"
        n.opacity = 1.
        n.margin_top = 2
        n.margin_right = 2
        n.margin_left = 2
        n.margin_bottom = 2

        lf.add_face(n, column_add-2, position="aligned")

        # add mecA typing
        if head:
            n = TextFace(' mecA ')
            n.margin_top = 2
            n.margin_right = 2
            n.margin_left = 2
            n.margin_bottom = 2
            n.rotation = 270
            n.vt_align = 2
            n.hz_align = 2
            n.inner_background.color = "white"
            n.opacity = 1.
            tss.aligned_header.add_face(n, column_add-1)
        if lf.name in leaf_id2meca:
            n = TextFace(' %s ' % leaf_id2meca[lf.name])
            if leaf_id2meca[lf.name] == 'Perfect':
                n.inner_background.color = "red"
            elif leaf_id2meca[lf.name] == 'Strict':
                n.inner_background.color = "orange"
            else:
                n.inner_background.color = "white"
        else:
            n = TextFace('   na   ')
            n.inner_background.color = "grey"
        n.opacity = 1.
        n.margin_top = 2
        n.margin_right = 2
        n.margin_left = 2
        n.margin_bottom = 2

        lf.add_face(n, column_add-1, position="aligned")

        # loop to add virulence gene hits
        for column, protein_id in enumerate(ordered_queries):
            # draw labels at the top of each column
            if head:
                if show_identity_values:
                    n = TextFace(' %s ' % str(protein_id))
                    n.margin_top = 2
                    n.margin_right = 2
                    n.margin_left = 2
                    n.margin_bottom = 2
                    n.rotation = 270
                    n.vt_align = 2
                    n.hz_align = 2
                    n.inner_background.color = "white"
                    n.opacity = 1.
                    tss.aligned_header.add_face(n, column+column_add)
                else:
                    n = TextFace(' %s ' % str(protein_id), fsize=6)
                    n.margin_top = 0
                    n.margin_right = 0
                    n.margin_left = 0
                    n.margin_bottom = 0
                    n.rotation = 270
                    n.vt_align = 2
                    n.hz_align = 2
                    n.inner_background.color = "white"
                    n.opacity = 1.
                    # lf.add_face(n, col, position="aligned")
                    tss.aligned_header.add_face(n, column+column_add)
            # draw column content
            if lf.name not in leaf_id2protein_id2identity:
                n = TextFace(' %s ' % str('  na  '))
                n.opacity = 1.
                n.margin_top = 2
                n.margin_right = 2
                n.margin_left = 2
                n.margin_bottom = 2
                n.inner_background.color = "grey"
                lf.add_face(n, column+column_add, position="aligned")
            else:
                if protein_id in leaf_id2protein_id2identity[lf.name]:
                    identity_value = float(leaf_id2protein_id2identity[lf.name][protein_id])
                    color = rgb2hex(m_blue.to_rgba(identity_value))


                    if show_identity_values:
                        # report identity values in coloured boxes
                        # adapt box size depending the digit width
                        if str(identity_value) == '100.00' or str(identity_value) == '100.0':
                            identity_value = '100'
                            n = TextFace(" %s  " % identity_value)
                        else:
                            n = TextFace("%.2f" % round(float(identity_value), 2))
                        # color text to white for dark cells
                        if float(identity_value) > 95:
                            n.fgcolor = "white"
                        n.opacity = 1.
                        n.margin_top = 2
                        n.margin_right = 2
                        n.margin_left = 2
                        n.margin_bottom = 2
                        n.inner_background.color = color
                        lf.add_face(n, column+column_add, position="aligned")
                    else:
                        # draw coloured boxes without text
                        n = TextFace('  ')
                        n.margin_top = 0
                        n.margin_right = 0
                        n.margin_left = 0
                        n.margin_bottom = 0
                        # n.color = color
                        n.inner_background.color = color
                        lf.add_face(n, column+column_add, position="aligned")
                else:
                    n = TextFace('  %s  ' % str('  -  '))
                    n.opacity = 1.
                    n.margin_top = 2
                    n.margin_right = 2
                    n.margin_left = 2
                    n.margin_bottom = 2
                    n.inner_background.color = "white"
                    lf.add_face(n, column+column_add, position="aligned")

        # end of first leaf: turn off header
        head = False

    # add boostrap supports
    for n in t1.traverse():
        nstyle = NodeStyle()
        if n.support < 0.9:
            nstyle["fgcolor"] = "blue"
            nstyle["size"] = 6
            n.set_style(nstyle)
        else:
            nstyle["fgcolor"] = "red"
            nstyle["size"] = 0
            n.set_style(nstyle)

    return t1, tss
Ejemplo n.º 24
0
 def set_curr_node_value(self, text):
     tf = TextFace(text, fsize=9)
     tf.margin_right = 6
     self.curr_node.add_face(tf, column=0, position='branch-right')
Ejemplo n.º 25
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
Ejemplo n.º 26
0
        manual_mode_nodes["T"].append(l_e[0])
        manual_mode_nodes["C"].extend(l_e[1:])

for n in tree.traverse():
    if n.is_leaf():
        n.set_style(nstyle_L)
        n.add_face(TextFace(str(n.name)), column=0, position="aligned")
    else:
        n.set_style(nstyle)
    nd = TextFace(str(n.ND))

    if manual_mode_nodes:
        if n.ND in manual_mode_nodes["T"]:
            nd.background.color = "red"
        elif n.ND in manual_mode_nodes["C"]:
            nd.background.color = "orange"
        else:
            nd.background.color = "white"
    else:
        nd.background.color = "white"
    nd.margin_right = 2
    nd.margin_top = 1
    nd.margin_left = 2
    nd.margin_bottom = 1
    nd.border.width = 1
    n.add_face(nd, column=0, position="float")
    n.add_face(TextFace("       "), column=0, position="branch-bottom")

tree.render(args.output, tree_style=tree_style)
print args.output
Ejemplo n.º 27
0
t = Tree(t[0])

ts = TreeStyle()
ts.show_leaf_name = False
ts.show_branch_length = False
ts.show_branch_support = True

ts.scale = 2
#ts.min_leaf_separation = 3
ts.branch_vertical_margin = 12

ts.legend_position = 4
#ts.legend.add_face(CircleFace(3, "red"), column=0)
mark = TextFace("Outbreak", fsize=10, fgcolor=outbreak_color)
mark.margin_top = 10
mark.margin_right = 10
mark.margin_left = 5
mark.margin_bottom = 10
#ts.legend.add_face(mark, column=1)

mark2 = TextFace("X", fsize=10, fgcolor="black")
# Set some attributes
mark2.margin_top = 0
mark2.margin_right = 1
mark2.margin_left = 1
mark2.margin_bottom = 0
mark2.opacity = 1  # from 0 to 1
mark2.border.width = 1
mark2.background.color = "#F5F5DC"
ts.legend.add_face(mark2, column=0)
Ejemplo n.º 28
0
def plot_tree_barplot(tree_file,
                      taxon2value_list_barplot,
                      header_list,
                      taxon2set2value_heatmap=False,
                      header_list2=False,
                      column_scale=True,
                      general_max=False,
                      barplot2percentage=False,
                      taxon2mlst=False):
    '''

    display one or more barplot

    :param tree_file:
    :param taxon2value_list:
    :param exclude_outgroup:
    :param bw_scale:
    :param barplot2percentage: list of bool to indicates if the number are percentages and the range should be set to 0-100

    :return:
    '''

    import matplotlib.cm as cm
    from matplotlib.colors import rgb2hex
    import matplotlib as mpl

    if taxon2mlst:
        mlst_list = list(set(taxon2mlst.values()))
        mlst2color = dict(zip(mlst_list, get_spaced_colors(len(mlst_list))))
        mlst2color['-'] = 'white'

    if isinstance(tree_file, Tree):
        t1 = tree_file
    else:
        t1 = Tree(tree_file)

    # Calculate the midpoint node
    R = t1.get_midpoint_outgroup()
    # and set it as tree outgroup
    t1.set_outgroup(R)

    tss = TreeStyle()
    value = 1
    tss.draw_guiding_lines = True
    tss.guiding_lines_color = "gray"
    tss.show_leaf_name = False

    if column_scale and header_list2:
        import matplotlib.cm as cm
        from matplotlib.colors import rgb2hex
        import matplotlib as mpl
        column2scale = {}
        for column in header_list2:
            values = taxon2set2value_heatmap[column].values()

            norm = mpl.colors.Normalize(vmin=min(values), vmax=max(values))
            cmap = cm.OrRd
            m = cm.ScalarMappable(norm=norm, cmap=cmap)
            column2scale[column] = m

    cmap = cm.YlGnBu  #YlOrRd#OrRd

    values_lists = taxon2value_list_barplot.values()

    scale_list = []
    max_value_list = []

    for n, header in enumerate(header_list):
        #print 'scale', n, header
        data = [float(i[n]) for i in values_lists]

        if barplot2percentage is False:
            max_value = max(data)  #3424182#
            min_value = min(data)  #48.23
        else:
            if barplot2percentage[n] is True:
                max_value = 100
                min_value = 0
            else:
                max_value = max(data)  #3424182#
                min_value = min(data)  #48.23
        norm = mpl.colors.Normalize(vmin=min_value, vmax=max_value)
        m1 = cm.ScalarMappable(norm=norm, cmap=cmap)
        scale_list.append(m1)
        if not general_max:
            max_value_list.append(float(max_value))
        else:
            max_value_list.append(general_max)

    for i, lf in enumerate(t1.iter_leaves()):

        #if taxon2description[lf.name] == 'Pirellula staleyi DSM 6068':
        #    lf.name = 'Pirellula staleyi DSM 6068'
        #    continue
        if i == 0:

            col_add = 0

            if taxon2mlst:
                header_list = ['MLST'] + header_list

            for col, header in enumerate(header_list):

                #lf.add_face(n, column, position="aligned")
                n = TextFace(' ')
                n.margin_top = 1
                n.margin_right = 2
                n.margin_left = 2
                n.margin_bottom = 1
                n.rotation = 90
                n.inner_background.color = "white"
                n.opacity = 1.
                n.hz_align = 2
                n.vt_align = 2

                tss.aligned_header.add_face(n, col_add + 1)

                n = TextFace('%s' % header)
                n.margin_top = 1
                n.margin_right = 2
                n.margin_left = 2
                n.margin_bottom = 2
                n.rotation = 270
                n.inner_background.color = "white"
                n.opacity = 1.
                n.hz_align = 2
                n.vt_align = 1
                tss.aligned_header.add_face(n, col_add)
                col_add += 2

            if header_list2:
                for col, header in enumerate(header_list2):
                    n = TextFace('%s' % header)
                    n.margin_top = 1
                    n.margin_right = 20
                    n.margin_left = 2
                    n.margin_bottom = 1
                    n.rotation = 270
                    n.hz_align = 2
                    n.vt_align = 2
                    n.inner_background.color = "white"
                    n.opacity = 1.
                    tss.aligned_header.add_face(n, col + col_add)

        if taxon2mlst:

            try:
                #if lf.name in leaf2mlst or int(lf.name) in leaf2mlst:
                n = TextFace(' %s ' % taxon2mlst[int(lf.name)])
                n.inner_background.color = 'white'
                m = TextFace('  ')
                m.inner_background.color = mlst2color[taxon2mlst[int(lf.name)]]
            except:
                n = TextFace(' na ')
                n.inner_background.color = "grey"
                m = TextFace('    ')
                m.inner_background.color = "white"

            n.opacity = 1.
            n.margin_top = 2
            n.margin_right = 2
            n.margin_left = 0
            n.margin_bottom = 2

            m.margin_top = 2
            m.margin_right = 0
            m.margin_left = 2
            m.margin_bottom = 2

            lf.add_face(m, 0, position="aligned")
            lf.add_face(n, 1, position="aligned")
            col_add = 2
        else:
            col_add = 0

        try:
            val_list = taxon2value_list_barplot[lf.name]
        except:
            if not taxon2mlst:
                val_list = ['na'] * len(header_list)
            else:
                val_list = ['na'] * (len(header_list) - 1)

        for col, value in enumerate(val_list):

            # show value itself
            try:
                n = TextFace('  %s  ' % str(value))
            except:
                n = TextFace('  %s  ' % str(value))
            n.margin_top = 1
            n.margin_right = 5
            n.margin_left = 10
            n.margin_bottom = 1
            n.inner_background.color = "white"
            n.opacity = 1.

            lf.add_face(n, col_add, position="aligned")
            # show bar
            try:
                color = rgb2hex(scale_list[col].to_rgba(float(value)))
            except:
                color = 'white'
            try:
                percentage = (value / max_value_list[col]) * 100
                #percentage = value
            except:
                percentage = 0
            try:
                maximum_bar = (
                    (max_value_list[col] - value) / max_value_list[col]) * 100
            except:
                maximum_bar = 0
            #maximum_bar = 100-percentage
            b = StackedBarFace([percentage, maximum_bar],
                               width=100,
                               height=10,
                               colors=[color, "white"])
            b.rotation = 0
            b.inner_border.color = "grey"
            b.inner_border.width = 0
            b.margin_right = 15
            b.margin_left = 0
            lf.add_face(b, col_add + 1, position="aligned")
            col_add += 2

        if taxon2set2value_heatmap:
            shift = col + col_add + 1

            i = 0
            for col, col_name in enumerate(header_list2):
                try:
                    value = taxon2set2value_heatmap[col_name][lf.name]
                except:
                    try:
                        value = taxon2set2value_heatmap[col_name][int(lf.name)]
                    except:
                        value = 0

                if int(value) > 0:
                    if int(value) > 9:
                        n = TextFace(' %i ' % int(value))
                    else:
                        n = TextFace(' %i   ' % int(value))
                    n.margin_top = 1
                    n.margin_right = 1
                    n.margin_left = 20
                    n.margin_bottom = 1
                    n.fgcolor = "white"
                    n.inner_background.color = rgb2hex(
                        column2scale[col_name].to_rgba(
                            float(value)))  #"orange"
                    n.opacity = 1.
                    lf.add_face(n, col + col_add, position="aligned")
                    i += 1
                else:
                    n = TextFace('  ')  #% str(value))
                    n.margin_top = 1
                    n.margin_right = 1
                    n.margin_left = 20
                    n.margin_bottom = 1
                    n.inner_background.color = "white"
                    n.opacity = 1.

                    lf.add_face(n, col + col_add, position="aligned")

        n = TextFace(lf.name, fgcolor="black", fsize=12, fstyle='italic')
        lf.add_face(n, 0)

    for n in t1.traverse():
        nstyle = NodeStyle()
        if n.support < 1:
            nstyle["fgcolor"] = "black"
            nstyle["size"] = 6
            n.set_style(nstyle)
        else:
            nstyle["fgcolor"] = "red"
            nstyle["size"] = 0
            n.set_style(nstyle)

    return t1, tss
nstyle["fgcolor"] = "black"
nstyle["size"] = 1

MESSAGE("Setting tree style")
tree_style = TreeStyle()
tree_style.show_leaf_name = False
tree_style.show_branch_length = False
tree_style.draw_guiding_lines = True
tree_style.complete_branch_lines_when_necessary = True
tree_style.legend_position = 1

MESSAGE("Setting legend with condition numbers and colors")
for condi_i in sorted(condi_color_dic.keys()):
    tf = TextFace("Condition      " + condi_i)
    tf.background.color = condi_color_dic[condi_i]
    tf.margin_right = 2
    tf.margin_top = 1
    tf.margin_left = 2
    tf.margin_bottom = 1
    tf.border.width = 1
    tree_style.legend.add_face(tf, column=1)

if add_transition:
    MESSAGE("Setting transition style")
    tf = TextFace("Transition -> x")
    tf.background.color = "white"
    tf.margin_right = 2
    tf.margin_top = 1
    tf.margin_left = 2
    tf.margin_bottom = 1
    tf.border.color = "red"
Ejemplo n.º 30
0
def ete_layout(node):
    """
    Formatting of tree nodes while tree is rendered
    :param node: ete node
    """

    nstyle = NodeStyle()
    nstyle["shape"] = "sphere"
    nstyle["size"] = 0

    nstyle["vt_line_width"] = 1  # line width of vertical branches
    nstyle["hz_line_width"] = 1  # line width of horizontal branches

    node.set_style(nstyle)

    if 'median_vaf' in node.features and not np.isnan(
            node.median_vaf) and logger.isEnabledFor(logging.DEBUG):
        # # Creates a sphere face whose size is proportional to the given VAF or CCF of the acquired muts
        # cf = CircleFace(radius=node.median_vaf*50, color="RoyalBlue", style="sphere")
        # # Let's make the sphere transparent
        # cf.opacity = 0.6
        # # And place as a float face over the tree
        # faces.add_face_to_node(cf, node, column=0, position="float-behind")

        vaf_face = TextFace('({:.0%}) '.format(node.median_vaf),
                            fsize=9,
                            fgcolor='SlateGray')
        vaf_face.opacity = 0.8  # from 0 to 1
        faces.add_face_to_node(vaf_face, node, column=2, position="branch-top")

    if node.is_root():
        # tf_root = TextFace(node.name, fsize=14, fgcolor="Black")
        # tf_root.margin_bottom = 25
        # faces.add_face_to_node(tf_root, node, column=0, position='branch-right')
        return

    bt_face_muts = AttrFace("dist",
                            fsize=11,
                            fgcolor="Black",
                            text_prefix=' ',
                            text_suffix=' ',
                            formatter='%d')
    bt_face_muts.margin_bottom = 2
    bt_face_muts.margin_top = 2
    faces.add_face_to_node(bt_face_muts, node, column=1, position="branch-top")

    if 'drivers' in node.features and len(node.drivers) > 0:
        drivers_face = TextFace(node.drivers,
                                fsize=11,
                                fstyle='italic',
                                fgcolor='OrangeRed')
        drivers_face.opacity = 0.8  # from 0 to 1
        drivers_face.margin_left = 1
        drivers_face.margin_right = 1
        faces.add_face_to_node(drivers_face,
                               node,
                               column=0,
                               position="branch-top")

    # If node is a leaf, add the nodes name and a its scientific name
    if node.is_leaf():

        # for colors see: http://etetoolkit.org/docs/latest/reference/reference_treeview.html#color-names
        leaf_face = AttrFace("name",
                             fsize=14,
                             fgcolor="Black",
                             text_prefix=' ',
                             text_suffix=' ')
        if node.name.startswith('PT') or node.name.startswith(
                'Primary'):  # primary tumor sample
            leaf_face.fgcolor = 'SteelBlue'
        elif node.name.startswith('LiM'):  # liver met
            leaf_face.fgcolor = 'DarkOliveGreen'
        elif node.name.startswith('LuM'):  # lung met
            leaf_face.fgcolor = 'SaddleBrown'
        elif node.name.startswith('NoM'):  # lymph node met
            leaf_face.fgcolor = 'DarkViolet'
        elif node.name.startswith('PeM'):  # peritoneal met
            leaf_face.fgcolor = 'Purple'
        elif node.name.startswith('Met') or node.name.startswith(
                'M'):  # some metastasis
            leaf_face.fgcolor = 'Magenta'
        elif node.name.startswith('BrM'):  # brain met
            leaf_face.fgcolor = 'Crimson'

        leaf_face.border.type = 0
        leaf_face.border.width = 1
        leaf_face.margin_bottom = 1
        leaf_face.margin_top = 1
        faces.add_face_to_node(leaf_face, node,
                               column=1)  # , position="aligned"

    else:  # inner node
        # if not np.isnan(node.support):
        # bt_face_conf = AttrFace("support", fsize=12, fgcolor="DimGrey", text_prefix=' ',
        #                         text_suffix=' ', formatter='%0.2f')
        if node.confidence is not None and node.confidence != '':
            tf_conf = TextFace(node.confidence, fsize=12, fgcolor='DimGrey')
            tf_conf.margin_left = 3
            tf_conf.margin_right = 3
            faces.add_face_to_node(tf_conf,
                                   node,
                                   column=1,
                                   position="branch-bottom")
Ejemplo n.º 31
0
    def add_simple_barplot(self, 
                           taxon2value, 
                           header_name,
                           color=False,
                           show_values=False,
                           substract_min=False,
                           max_value=False):

        print("scale factor", self.text_scale)

        if not show_values:
            self._add_header(header_name, column_add=0)
        else:
            self._add_header(header_name, column_add=1)
        
        
        values_lists = [float(i) for i in taxon2value.values()]
        
        min_value = min(values_lists)
        
        if substract_min:
            values_lists = [i-min_value for i in values_lists]
            for taxon in list(taxon2value.keys()):
                taxon2value[taxon] = taxon2value[taxon]-min_value
            
        if not color:
            color = self._get_default_barplot_color()
                
        for i, lf in enumerate(self.tree.iter_leaves()):

            try:
                value = taxon2value[lf.name]
            except:
                value = 0

            if show_values:
                barplot_column = 1
                if isinstance(value, float):
                    a = TextFace(" %s " % str(round(value,2)))
                else:
                    a = TextFace(" %s " % str(value))
                a.margin_top = 1
                a.margin_right = 2
                a.margin_left = 5
                a.margin_bottom = 1
                if self.rotate:
                    a.rotation = 270
                lf.add_face(a, self.column_count, position="aligned")
            else:
                barplot_column = 0
            if not max_value:
                fraction_biggest = (float(value)/max(values_lists))*100
            else:
                fraction_biggest = (float(value)/max_value)*100
            fraction_rest = 100-fraction_biggest

            b = StackedBarFace([fraction_biggest, fraction_rest], 
                               width=100 * (self.text_scale/3), 
                               height=18,
                               colors=[color, 'white'])
            b.rotation= 0
            #b.inner_border.color = "grey"
            #b.inner_border.width = 0
            b.margin_right = 10
            b.margin_left = 10
            b.hz_align = 2
            b.vt_align = 2
            b.rotable = False
            if self.rotate:
                b.rotation = 270
            lf.add_face(b, self.column_count + barplot_column, position="aligned")

        self.column_count += (1 + barplot_column)
Ejemplo n.º 32
0
    def add_simple_barplot(self, 
                           taxon2value, 
                           header_name,
                           color=False,
                           show_values=False,
                           substract_min=False,
                           highlight_cutoff=False,
                           highlight_reverse=False,
                           max_value=False):

        if not show_values:
            self._add_header(header_name, column_add=0)
        else:
            self._add_header(header_name, column_add=1)
        
        values_lists = [float(i) for i in taxon2value.values()]

        min_value = min(values_lists)
        
        if substract_min:
            values_lists = [i-min_value for i in values_lists]
            for taxon in list(taxon2value.keys()):
                taxon2value[taxon] = taxon2value[taxon]-min_value

        if not color:
            color = self._get_default_barplot_color()
                
        for i, lf in enumerate(self.tree.iter_leaves()):

            try:
                value = taxon2value[lf.name]
            except KeyError:
                value = 0

            if show_values:
                barplot_column = 1
                if substract_min:
                    real_value = value + min_value
                else:
                    real_value = value
                if isinstance(real_value, float):
                    a = TextFace(" %s " % str(round(real_value,2)))
                else:
                    a = TextFace(" %s " % str(real_value))
                a.margin_top = 1
                a.margin_right = 2
                a.margin_left = 5
                a.margin_bottom = 1
                if self.rotate:
                    a.rotation = 270
                lf.add_face(a, self.column_count, position="aligned")
            else:
                barplot_column = 0
            if not max_value:
                fraction_biggest = (float(value)/max(values_lists))*100
            else:
                fraction_biggest = (float(value)/max_value)*100
            fraction_rest = 100-fraction_biggest

            if highlight_cutoff:
                if substract_min:
                    real_value = value + min_value
                else:
                    real_value = value
                if highlight_reverse:
                    if real_value > highlight_cutoff:
                        lcolor = "grey"
                    else:
                        lcolor = color
                else:
                    if real_value < highlight_cutoff:
                        lcolor = "grey"
                    else:
                        lcolor = color
            else:
                lcolor = color
            
            b = StackedBarFace([fraction_biggest, fraction_rest], width=100, height=15,colors=[lcolor, 'white'])
            b.rotation= 0
            b.inner_border.color = "grey"
            b.inner_border.width = 0
            b.margin_right = 15
            b.margin_left = 0
            if self.rotate:
                b.rotation = 270
            lf.add_face(b, self.column_count + barplot_column, position="aligned")

        self.column_count += (1 + barplot_column)
Ejemplo n.º 33
0
def plot_phylum_counts(NOG_id,
                       rank='phylum',
                       colapse_low_species_counts=4,
                       remove_unlassified=True):
    '''

    1. get phylum tree
    2. foreach species => get phylum
    3. build phylum2count dictionnary
    3. plot barchart

    # merge eukaryotes into 5 main clades
    # merge virus as a single clade


    ATTENTION: no-rank groups and no-rank species...

    '''

    import MySQLdb
    import os
    from chlamdb.biosqldb import manipulate_biosqldb
    from ete3 import NCBITaxa, Tree, TextFace, TreeStyle, StackedBarFace
    ncbi = NCBITaxa()

    sqlpsw = os.environ['SQLPSW']
    conn = MySQLdb.connect(
        host="localhost",  # your host, usually localhost
        user="******",  # your username
        passwd=sqlpsw,  # your password
        db="eggnog")  # name of the data base
    cursor = conn.cursor()

    sql = 'select * from eggnog.leaf2n_genomes_%s' % rank

    cursor.execute(sql, )
    leaf_taxon2n_species = manipulate_biosqldb.to_dict(cursor.fetchall())

    leaf_taxon2n_species_with_domain = get_NOG_taxonomy(NOG_id, rank)

    sql = 'select phylogeny from eggnog.phylogeny where rank="%s"' % (rank)

    cursor.execute(sql, )
    tree = Tree(cursor.fetchall()[0][0], format=1)

    sql = 'select * from eggnog.taxid2label_%s' % rank
    cursor.execute(sql, )

    taxon_id2scientific_name_and_rank = manipulate_biosqldb.to_dict(
        cursor.fetchall())
    taxon_id2scientific_name_and_rank = {
        str(k): v
        for k, v in taxon_id2scientific_name_and_rank.items()
    }

    tss = TreeStyle()
    tss.draw_guiding_lines = True
    tss.guiding_lines_color = "blue"

    keep = []
    for lf in tree.iter_leaves():
        # n genomes

        if remove_unlassified:
            label = taxon_id2scientific_name_and_rank[str(lf.name)][0]
            if 'unclassified' in label:
                continue

        n_genomes = int(leaf_taxon2n_species[lf.name])
        if n_genomes > colapse_low_species_counts:
            keep.append(lf.name)
    print('number of leaaves:', len(keep))

    tree.prune(keep)

    header_list = ['Rank', 'N genomes', 'N with %s' % NOG_id, 'Percentage']
    for col, header in enumerate(header_list):

        n = TextFace('%s' % (header))
        n.margin_top = 0
        n.margin_right = 1
        n.margin_left = 20
        n.margin_bottom = 1
        n.rotation = 270
        n.hz_align = 2
        n.vt_align = 2
        n.inner_background.color = "white"
        n.opacity = 1.
        tss.aligned_header.add_face(n, col)

    for lf in tree.iter_leaves():
        # n genomes

        n_genomes = int(leaf_taxon2n_species[lf.name])
        if n_genomes <= colapse_low_species_counts:
            continue

        n = TextFace('  %s ' % str(leaf_taxon2n_species[lf.name]))
        n.margin_top = 1
        n.margin_right = 1
        n.margin_left = 0
        n.margin_bottom = 1
        n.fsize = 7
        n.inner_background.color = "white"
        n.opacity = 1.
        lf.add_face(n, 2, position="aligned")

        # n genomes with domain
        try:
            m = TextFace('  %s ' %
                         str(leaf_taxon2n_species_with_domain[lf.name]))
        except:
            m = TextFace('  0 ')
        m.margin_top = 1
        m.margin_right = 1
        m.margin_left = 0
        m.margin_bottom = 1
        m.fsize = 7
        m.inner_background.color = "white"
        m.opacity = 1.
        lf.add_face(m, 3, position="aligned")

        # rank
        ranks = ncbi.get_rank([lf.name])
        try:
            r = ranks[max(ranks.keys())]
        except:
            r = '-'
        n = TextFace('  %s ' % r, fsize=14, fgcolor='red')
        n.margin_top = 1
        n.margin_right = 1
        n.margin_left = 0
        n.margin_bottom = 1
        n.fsize = 7
        n.inner_background.color = "white"
        n.opacity = 1.
        lf.add_face(n, 1, position="aligned")

        # percent with target domain
        try:
            percentage = (float(leaf_taxon2n_species_with_domain[lf.name]) /
                          float(leaf_taxon2n_species[lf.name])) * 100
        except:
            percentage = 0
        m = TextFace('  %s ' % str(round(percentage, 2)))
        m.fsize = 1
        m.margin_top = 1
        m.margin_right = 1
        m.margin_left = 0
        m.margin_bottom = 1
        m.fsize = 7
        m.inner_background.color = "white"
        m.opacity = 1.
        lf.add_face(m, 4, position="aligned")

        b = StackedBarFace([percentage, 100 - percentage],
                           width=100,
                           height=10,
                           colors=["#7fc97f", "white"])
        b.rotation = 0
        b.inner_border.color = "grey"
        b.inner_border.width = 0
        b.margin_right = 15
        b.margin_left = 0
        lf.add_face(b, 5, position="aligned")

        n = TextFace('%s' % taxon_id2scientific_name_and_rank[str(lf.name)][0],
                     fgcolor="black",
                     fsize=9)  # , fstyle = 'italic'

        lf.name = " %s (%s)" % (taxon_id2scientific_name_and_rank[str(
            lf.name)][0], str(lf.name))
        n.margin_right = 10
        lf.add_face(n, 0)

    tss.show_leaf_name = False

    for node in tree.traverse("postorder"):
        try:
            r = taxon_id2scientific_name_and_rank[str(node.name)][1]
        except:
            pass
        try:
            if r in ['phylum', 'superkingdom', 'class', 'subphylum'
                     ] or taxon_id2scientific_name_and_rank[str(
                         node.name)][0] in ['FCB group']:

                hola = TextFace(
                    "%s" %
                    (taxon_id2scientific_name_and_rank[str(node.name)][0]))
                node.add_face(hola, column=0, position="branch-top")
        except:
            pass
    return tree, tss