Example #1
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
Example #2
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)
Example #3
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
Example #4
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
Example #5
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
Example #6
0
def plot_heat_tree(biodb, taxid2n, tree_file):
    '''
    Plot heatmap next to a tree. The order of the heatmap **MUST** be the same,
    as order of the leafs on the tree. The tree must be in the Newick format. If
    *output_file* is specified, then heat-tree will be rendered as a PNG,
    otherwise interactive browser will pop-up with your heat-tree.

    Parameters
    ----------
    heatmap_file: str
        Path to the heatmap file. The first row must have '#Names' as first
        element of the header.
            e.g. #Names, A, B, C, D
                row1, 2, 4, 0, 4
                row2, 4, 6, 2, -1

    tree_file: str
        Path to the tree file in Newick format. The leaf node labels should
        be the same as as row names in the heatmap file. E.g. row1, row2.

    output_file: str, optional
        If specified the heat-tree will be rendered in that file as a PNG image,
        otherwise interactive browser will pop-up. **N.B.** program will wait
        for you to exit the browser before continuing.
    '''

    from chlamdb.biosqldb import manipulate_biosqldb
    server, db = manipulate_biosqldb.load_db(biodb)

    taxid2organism = manipulate_biosqldb.taxon_id2genome_description(
        server, biodb, True)

    t1 = Tree(tree_file)

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

    leaf_number = 0
    for lf in t1.iter_leaves():
        leaf_number += 1
        lf.branch_vertical_margin = 0
        try:
            data = [taxid2n[str(lf.name)]]
        except:
            data = [0]
        #print 'taxon', int(lf.name)
        lf.name = taxid2organism[int(lf.name)]
        for col, value in enumerate(data):
            if value > 0:
                n = TextFace(' %s ' % str(value))
                n.margin_top = 2
                n.margin_right = 2
                n.margin_left = 2
                n.margin_bottom = 2
                n.inner_background.color = "#81BEF7"
                n.opacity = 1.
                lf.add_face(n, col, position="aligned")

            else:
                n = TextFace(' %s ' % str(value))
                n.margin_top = 2
                n.margin_right = 2
                n.margin_left = 2
                n.margin_bottom = 2
                n.inner_background.color = "white"
                n.opacity = 1.
                lf.add_face(n, col, position="aligned")

    return t1, leaf_number
Example #7
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
Example #8
0
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)

mark3 = TextFace("Selected branches", fsize=10, fgcolor="black")
mark3.margin_top = 2
mark3.margin_right = 20
mark3.margin_left = 5
mark3.margin_bottom = 2
ts.legend.add_face(mark3, column=1)

ts.margin_left = 20
ts.margin_right = 20
ts.margin_top = 10
ts.margin_bottom = 10
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
Example #10
0
def plot_heat_tree(tree_file,
                   biodb="chlamydia_04_16",
                   exclude_outgroup=False,
                   bw_scale=True):
    from chlamdb.biosqldb import manipulate_biosqldb
    import matplotlib.cm as cm
    from matplotlib.colors import rgb2hex
    import matplotlib as mpl

    server, db = manipulate_biosqldb.load_db(biodb)

    sql_biodatabase_id = 'select biodatabase_id from biodatabase where name="%s"' % biodb
    db_id = server.adaptor.execute_and_fetchall(sql_biodatabase_id, )[0][0]
    if type(tree_file) == str:
        t1 = Tree(tree_file)
        try:
            R = t1.get_midpoint_outgroup()
            #print 'root', R
            # and set it as tree outgroup
            t1.set_outgroup(R)
        except:
            pass
    elif isinstance(tree_file, Tree):
        t1 = tree_file
    else:
        IOError('Unkown tree format')
    tss = TreeStyle()
    tss.draw_guiding_lines = True
    tss.guiding_lines_color = "gray"
    tss.show_leaf_name = False

    #print "tree", t1

    sql1 = 'select taxon_id, description from bioentry where biodatabase_id=%s and description not like "%%%%plasmid%%%%"' % db_id
    sql2 = 'select t2.taxon_id, t1.GC from genomes_info_%s as t1 inner join bioentry as t2 ' \
           ' on t1.accession=t2.accession where t2.biodatabase_id=%s and t1.description not like "%%%%plasmid%%%%";' % (biodb, db_id)
    sql3 = 'select t2.taxon_id, t1.genome_size from genomes_info_%s as t1 ' \
           ' inner join bioentry as t2 on t1.accession=t2.accession ' \
           ' where t2.biodatabase_id=%s and t1.description not like "%%%%plasmid%%%%";' % (biodb, db_id)
    sql4 = 'select t2.taxon_id,percent_non_coding from genomes_info_%s as t1 ' \
           ' inner join bioentry as t2 on t1.accession=t2.accession ' \
           ' where t2.biodatabase_id=%s and t1.description not like "%%%%plasmid%%%%";' % (biodb, db_id)

    sql_checkm_completeness = 'select taxon_id, completeness from custom_tables.checkm_%s;' % biodb
    sql_checkm_contamination = 'select taxon_id,contamination from custom_tables.checkm_%s;' % biodb

    try:
        taxon_id2completeness = manipulate_biosqldb.to_dict(
            server.adaptor.execute_and_fetchall(sql_checkm_completeness))
        taxon_id2contamination = manipulate_biosqldb.to_dict(
            server.adaptor.execute_and_fetchall(sql_checkm_contamination))
    except:
        taxon_id2completeness = False
    #taxon2description = manipulate_biosqldb.to_dict(server.adaptor.execute_and_fetchall(sql1,))

    taxon2description = manipulate_biosqldb.taxon_id2genome_description(
        server, biodb, filter_names=True)

    taxon2gc = manipulate_biosqldb.to_dict(
        server.adaptor.execute_and_fetchall(sql2, ))
    taxon2genome_size = manipulate_biosqldb.to_dict(
        server.adaptor.execute_and_fetchall(sql3, ))
    taxon2coding_density = manipulate_biosqldb.to_dict(
        server.adaptor.execute_and_fetchall(sql4, ))

    my_taxons = [lf.name for lf in t1.iter_leaves()]

    # Calculate the midpoint node

    if exclude_outgroup:
        excluded = str(list(t1.iter_leaves())[0].name)
        my_taxons.pop(my_taxons.index(excluded))

    genome_sizes = [float(taxon2genome_size[i]) for i in my_taxons]
    gc_list = [float(taxon2gc[i]) for i in my_taxons]
    fraction_list = [float(taxon2coding_density[i]) for i in my_taxons]

    value = 1

    max_genome_size = max(genome_sizes)  #3424182#
    max_gc = max(gc_list)  #48.23

    cmap = cm.YlGnBu  #YlOrRd#OrRd

    norm = mpl.colors.Normalize(vmin=min(genome_sizes) - 100000,
                                vmax=max(genome_sizes))
    m1 = cm.ScalarMappable(norm=norm, cmap=cmap)
    norm = mpl.colors.Normalize(vmin=min(gc_list), vmax=max(gc_list))
    m2 = cm.ScalarMappable(norm=norm, cmap=cmap)
    norm = mpl.colors.Normalize(vmin=min(fraction_list),
                                vmax=max(fraction_list))
    m3 = cm.ScalarMappable(norm=norm, cmap=cmap)

    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:
            n = TextFace('Size (Mbp)')
            n.rotation = -25
            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, 3, position="aligned")
            tss.aligned_header.add_face(n, 3)
            n = TextFace('GC (%)')
            n.rotation = -25
            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, 5, position="aligned")
            tss.aligned_header.add_face(n, 5)
            n = TextFace('')
            #lf.add_face(n, 2, position="aligned")
            tss.aligned_header.add_face(n, 2)
            #lf.add_face(n, 4, position="aligned")
            tss.aligned_header.add_face(n, 4)
            n = TextFace('Non coding (%)')
            n.margin_top = 1
            n.margin_right = 1
            n.margin_left = 20
            n.margin_bottom = 1
            n.inner_background.color = "white"
            n.opacity = 1.
            n.rotation = -25
            #lf.add_face(n, 7, position="aligned")
            tss.aligned_header.add_face(n, 7)
            n = TextFace('')
            #lf.add_face(n, 6, position="aligned")
            tss.aligned_header.add_face(n, 6)

            if taxon_id2completeness:
                n = TextFace('Completeness (%)')
                n.margin_top = 1
                n.margin_right = 1
                n.margin_left = 20
                n.margin_bottom = 1
                n.inner_background.color = "white"
                n.opacity = 1.
                n.rotation = -25
                #lf.add_face(n, 7, position="aligned")
                tss.aligned_header.add_face(n, 9)
                n = TextFace('')
                #lf.add_face(n, 6, position="aligned")
                tss.aligned_header.add_face(n, 8)

                n = TextFace('Contamination (%)')
                n.margin_top = 1
                n.margin_right = 1
                n.margin_left = 20
                n.margin_bottom = 1
                n.inner_background.color = "white"
                n.opacity = 1.
                n.rotation = -25
                #lf.add_face(n, 7, position="aligned")
                tss.aligned_header.add_face(n, 11)
                n = TextFace('')
                #lf.add_face(n, 6, position="aligned")
                tss.aligned_header.add_face(n, 10)

        value += 1

        #print '------ %s' % lf.name
        if exclude_outgroup and i == 0:
            lf.name = taxon2description[lf.name]
            #print '#######################'
            continue

        n = TextFace(
            '  %s ' %
            str(round(taxon2genome_size[lf.name] / float(1000000), 2)))
        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")
        #if max_genome_size > 3424182:
        #    max_genome_size = 3424182
        fraction_biggest = (float(taxon2genome_size[lf.name]) /
                            max_genome_size) * 100
        fraction_rest = 100 - fraction_biggest
        if taxon2description[lf.name] == 'Rhabdochlamydia helveticae T3358':
            col = '#fc8d59'
        else:
            if not bw_scale:
                col = rgb2hex(m1.to_rgba(float(
                    taxon2genome_size[lf.name])))  # 'grey'
            else:
                col = '#fc8d59'

        b = StackedBarFace([fraction_biggest, fraction_rest],
                           width=100,
                           height=9,
                           colors=[col, 'white'])
        b.rotation = 0
        b.inner_border.color = "black"
        b.inner_border.width = 0
        b.margin_right = 15
        b.margin_left = 0
        lf.add_face(b, 3, position="aligned")

        fraction_biggest = (float(taxon2gc[lf.name]) / max_gc) * 100
        fraction_rest = 100 - fraction_biggest
        if taxon2description[lf.name] == 'Rhabdochlamydia helveticae T3358':
            col = '#91bfdb'
        else:
            if not bw_scale:
                col = rgb2hex(m2.to_rgba(float(taxon2gc[lf.name])))
            else:
                col = '#91bfdb'
        b = StackedBarFace([fraction_biggest, fraction_rest],
                           width=100,
                           height=9,
                           colors=[col, 'white'])
        b.rotation = 0
        b.inner_border.color = "black"
        b.inner_border.width = 0
        b.margin_left = 0
        b.margin_right = 15

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

        if taxon2description[lf.name] == 'Rhabdochlamydia helveticae T3358':
            col = '#99d594'
        else:
            if not bw_scale:
                col = rgb2hex(m3.to_rgba(float(taxon2coding_density[lf.name])))
            else:
                col = '#99d594'
        n = TextFace('  %s ' % str(float(taxon2coding_density[lf.name])))
        n.margin_top = 1
        n.margin_right = 0
        n.margin_left = 0
        n.margin_right = 0
        n.margin_bottom = 1
        n.fsize = 7
        n.inner_background.color = "white"
        n.opacity = 1.
        lf.add_face(n, 6, position="aligned")
        fraction = (float(taxon2coding_density[lf.name]) /
                    max(taxon2coding_density.values())) * 100
        fraction_rest = ((max(taxon2coding_density.values()) -
                          taxon2coding_density[lf.name]) /
                         float(max(taxon2coding_density.values()))) * 100
        #print 'fraction, rest', fraction, fraction_rest
        b = StackedBarFace(
            [fraction, fraction_rest],
            width=100,
            height=9,
            colors=[col, 'white'
                    ])  # 1-round(float(taxon2coding_density[lf.name]), 2)
        b.rotation = 0
        b.margin_right = 1
        b.inner_border.color = "black"
        b.inner_border.width = 0
        b.margin_left = 5
        lf.add_face(b, 7, position="aligned")

        if taxon_id2completeness:
            n = TextFace('  %s ' % str(float(taxon_id2completeness[lf.name])))
            n.margin_top = 1
            n.margin_right = 0
            n.margin_left = 0
            n.margin_right = 0
            n.margin_bottom = 1
            n.fsize = 7
            n.inner_background.color = "white"
            n.opacity = 1.
            lf.add_face(n, 8, position="aligned")
            fraction = float(taxon_id2completeness[lf.name])
            fraction_rest = 100 - fraction
            #print 'fraction, rest', fraction, fraction_rest
            b = StackedBarFace(
                [fraction, fraction_rest],
                width=100,
                height=9,
                colors=["#d7191c", 'white'
                        ])  # 1-round(float(taxon2coding_density[lf.name]), 2)
            b.rotation = 0
            b.margin_right = 1
            b.inner_border.color = "black"
            b.inner_border.width = 0
            b.margin_left = 5
            lf.add_face(b, 9, position="aligned")

            n = TextFace('  %s ' % str(float(taxon_id2contamination[lf.name])))
            n.margin_top = 1
            n.margin_right = 0
            n.margin_left = 0
            n.margin_right = 0
            n.margin_bottom = 1
            n.fsize = 7
            n.inner_background.color = "white"
            n.opacity = 1.
            lf.add_face(n, 10, position="aligned")
            fraction = float(taxon_id2contamination[lf.name])
            fraction_rest = 100 - fraction
            #print 'fraction, rest', fraction, fraction_rest
            b = StackedBarFace(
                [fraction, fraction_rest],
                width=100,
                height=9,
                colors=["black", 'white'
                        ])  # 1-round(float(taxon2coding_density[lf.name]), 2)
            b.rotation = 0
            b.margin_right = 1
            b.inner_border.color = "black"
            b.inner_border.width = 0
            b.margin_left = 5
            lf.add_face(b, 11, position="aligned")

            #lf.name = taxon2description[lf.name]
        n = TextFace(taxon2description[lf.name],
                     fgcolor="black",
                     fsize=9,
                     fstyle='italic')
        n.margin_right = 30
        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
Example #11
0
def plot_tree_stacked_barplot(
        tree_file,
        taxon2value_list_barplot=False,
        header_list=False,  # header stackedbarplots
        taxon2set2value_heatmap=False,
        taxon2label=False,
        header_list2=False,  # header counts columns
        biodb=False,
        column_scale=True,
        general_max=False,
        header_list3=False,
        set2taxon2value_list_simple_barplot=False,
        set2taxon2value_list_simple_barplot_counts=True,
        rotate=False,
        taxon2description=False):
    '''

    taxon2value_list_barplot list of lists:
    [[bar1_part1, bar1_part2,...],[bar2_part1, bar2_part2]]
    valeures de chaque liste transformes en pourcentages

    :param tree_file:
    :param taxon2value_list:
    :param biodb:
    :param exclude_outgroup:
    :param bw_scale:
    :return:
    '''

    if biodb:
        from chlamdb.biosqldb import manipulate_biosqldb
        server, db = manipulate_biosqldb.load_db(biodb)

        taxon2description = manipulate_biosqldb.taxon_id2genome_description(
            server, biodb, filter_names=True)

    t1 = Tree(tree_file)

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

    colors2 = [
        "red", "#FFFF00", "#58FA58", "#819FF7", "#F781F3", "#2E2E2E",
        "#F7F8E0", 'black'
    ]
    colors = [
        "#7fc97f", "#386cb0", "#fdc086", "#ffffb3", "#fdb462", "#f0027f",
        "#F7F8E0", 'black'
    ]  # fdc086ff 386cb0ff f0027fff

    tss = TreeStyle()
    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 = {}
        col_n = 0
        for column in header_list2:
            values = taxon2set2value_heatmap[column].values()
            #print values
            if min(values) == max(values):
                min_val = 0
                max_val = 1.5 * max(values)
            else:
                min_val = min(values)
                max_val = max(values)
            #print 'min-max', min_val, max_val
            norm = mpl.colors.Normalize(vmin=min_val, vmax=max_val)  # *1.1
            if col_n < 4:
                cmap = cm.OrRd  #
            else:
                cmap = cm.YlGnBu  #PuBu#OrRd

            m = cm.ScalarMappable(norm=norm, cmap=cmap)

            column2scale[column] = [m, float(max_val)]  # *0.7
            col_n += 1

    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:

            if taxon2label:
                n = TextFace('  ')
                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.

                tss.aligned_header.add_face(n, 0)
                col_add = 1
            else:
                col_add = 1
            if header_list:
                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 + col_add)
                col_add += col + 1

            if header_list3:
                #print 'header_list 3!'
                col_tmp = 0
                for header in header_list3:
                    n = TextFace('%s' % (header))
                    n.margin_top = 1
                    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.

                    if set2taxon2value_list_simple_barplot_counts:
                        if col_tmp == 0:
                            col_tmp += 1
                        tss.aligned_header.add_face(n, col_tmp + 1 + col_add)
                        n = TextFace('       ')
                        tss.aligned_header.add_face(n, col_tmp + col_add)
                        col_tmp += 2
                    else:
                        tss.aligned_header.add_face(n, col_tmp + col_add)
                        col_tmp += 1
                if set2taxon2value_list_simple_barplot_counts:
                    col_add += col_tmp
                else:
                    col_add += col_tmp

            if header_list2:
                for col, header in enumerate(header_list2):
                    n = TextFace('%s' % (header))
                    n.margin_top = 1
                    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 + col_add)
                col_add += col + 1

        if taxon2label:
            try:
                n = TextFace('%s' % taxon2label[lf.name])
            except:
                try:
                    n = TextFace('%s' % taxon2label[int(lf.name)])
                except:
                    n = TextFace('-')
            n.margin_top = 1
            n.margin_right = 1
            n.margin_left = 20
            n.margin_bottom = 1
            n.inner_background.color = "white"
            n.opacity = 1.
            if rotate:
                n.rotation = 270
            lf.add_face(n, 1, position="aligned")
            col_add = 2
        else:
            col_add = 2

        if taxon2value_list_barplot:

            try:
                val_list_of_lists = taxon2value_list_barplot[lf.name]
            except:
                val_list_of_lists = taxon2value_list_barplot[int(lf.name)]

            #col_count = 0
            for col, value_list in enumerate(val_list_of_lists):

                total = float(sum(value_list))
                percentages = [(i / total) * 100 for i in value_list]
                if col % 3 == 0:
                    col_list = colors2
                else:
                    col_list = colors
                b = StackedBarFace(percentages,
                                   width=150,
                                   height=18,
                                   colors=col_list[0:len(percentages)])
                b.rotation = 0
                b.inner_border.color = "white"
                b.inner_border.width = 0
                b.margin_right = 5
                b.margin_left = 5
                if rotate:
                    b.rotation = 270
                lf.add_face(b, col + col_add, position="aligned")
                #col_count+=1

            col_add += col + 1

        if set2taxon2value_list_simple_barplot:
            col_list = [
                '#fc8d59', '#91bfdb', '#99d594', '#c51b7d', '#f1a340',
                '#999999'
            ]
            color_i = 0
            col = 0
            for one_set in header_list3:
                if color_i > 5:
                    color_i = 0
                color = col_list[color_i]
                color_i += 1
                # values for all taxons
                values_lists = [
                    float(i) for i in
                    set2taxon2value_list_simple_barplot[one_set].values()
                ]
                #print values_lists
                #print one_set
                value = set2taxon2value_list_simple_barplot[one_set][lf.name]

                if set2taxon2value_list_simple_barplot_counts:
                    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 rotate:
                        a.rotation = 270
                    lf.add_face(a, col + col_add, position="aligned")

                #print 'value and max', value, max(values_lists)
                fraction_biggest = (float(value) / max(values_lists)) * 100
                fraction_rest = 100 - fraction_biggest

                #print 'fractions', fraction_biggest, fraction_rest
                b = StackedBarFace([fraction_biggest, fraction_rest],
                                   width=100,
                                   height=15,
                                   colors=[color, 'white'])
                b.rotation = 0
                b.inner_border.color = "grey"
                b.inner_border.width = 0
                b.margin_right = 15
                b.margin_left = 0
                if rotate:
                    b.rotation = 270
                if set2taxon2value_list_simple_barplot_counts:
                    if col == 0:
                        col += 1
                    lf.add_face(b, col + 1 + col_add, position="aligned")
                    col += 2
                else:
                    lf.add_face(b, col + col_add, position="aligned")
                    col += 1
            if set2taxon2value_list_simple_barplot_counts:
                col_add += col

            else:
                col_add += col

        if taxon2set2value_heatmap:
            i = 0
            #if not taxon2label:
            #    col_add-=1
            for col2, head in enumerate(header_list2):

                col_name = header_list2[i]
                try:
                    value = taxon2set2value_heatmap[col_name][str(lf.name)]
                except:
                    try:
                        value = taxon2set2value_heatmap[col_name][round(
                            float(lf.name), 2)]
                    except:
                        value = 0
                if header_list2[i] == 'duplicates':
                    print('dupli', lf.name, value)
                #print 'val----------------', value
                if int(value) > 0:
                    if int(value) >= 10 and int(value) < 100:
                        n = TextFace('%4i' % value)
                    elif int(value) >= 100:
                        n = TextFace('%3i' % value)
                    else:

                        n = TextFace('%5i' % value)

                    n.margin_top = 1
                    n.margin_right = 2
                    n.margin_left = 5
                    n.margin_bottom = 1
                    n.hz_align = 1
                    n.vt_align = 1
                    if rotate:
                        n.rotation = 270
                    n.inner_background.color = rgb2hex(
                        column2scale[col_name][0].to_rgba(
                            float(value)))  #"orange"
                    #print 'xaxaxaxaxa', value,
                    if float(value) > column2scale[col_name][1]:

                        n.fgcolor = 'white'
                    n.opacity = 1.
                    n.hz_align = 1
                    n.vt_align = 1
                    lf.add_face(n, col2 + col_add, position="aligned")
                    i += 1
                else:
                    n = TextFace('')
                    n.margin_top = 1
                    n.margin_right = 1
                    n.margin_left = 5
                    n.margin_bottom = 1
                    n.inner_background.color = "white"
                    n.opacity = 1.
                    if rotate:
                        n.rotation = 270
                    lf.add_face(n, col2 + col_add, position="aligned")
                    i += 1

        #lf.name = taxon2description[lf.name]
        n = TextFace(taxon2description[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
Example #12
0
def plot_tree_text_metadata(tree_file, header2taxon2text, ordered_header_list,
                            biodb):

    from chlamdb.biosqldb import manipulate_biosqldb
    server, db = manipulate_biosqldb.load_db(biodb)

    t1 = Tree(tree_file)

    taxon2description = manipulate_biosqldb.taxon_id2genome_description(
        server, biodb, filter_names=True)

    # Calculate the midpoint node
    R = t1.get_midpoint_outgroup()
    # and set it as tree outgroup
    t1.set_outgroup(R)
    tss = TreeStyle()
    tss.draw_guiding_lines = True
    tss.guiding_lines_color = "gray"
    tss.show_leaf_name = False

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

        # first leaf, add headers
        if i == 0:
            for column, header in enumerate(ordered_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, column)
        for column, header in enumerate(ordered_header_list):
            text = header2taxon2text[header][int(leaf.name)]
            n = TextFace('%s' % text)
            n.margin_top = 1
            n.margin_right = 1
            n.margin_left = 5
            n.margin_bottom = 1
            n.inner_background.color = "white"
            n.opacity = 1.
            #n.rotation = 270
            leaf.add_face(n, column + 1, position="aligned")
        # rename leaf (taxon_id => description)
        n = TextFace(taxon2description[leaf.name],
                     fgcolor="black",
                     fsize=12,
                     fstyle='italic')
        leaf.add_face(n, 0)

    for n in t1.traverse():
        # rename leaf

        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
Example #13
0
def plot_heat_tree_V1(taxid2n,
                      tree_file,
                      genes,
                      taxid2st=False,
                      leaf_label_conversion_dico=False):
    '''
    Plot heatmap next to a tree. The order of the heatmap **MUST** be the same,
    as order of the leafs on the tree. The tree must be in the Newick format. If
    *output_file* is specified, then heat-tree will be rendered as a PNG,
    otherwise interactive browser will pop-up with your heat-tree.

    TODO ajouter en option la possibilite d'ajouter en option la valeur dans la cellule

    Parameters
    ----------


    tree_file: str
        Path to the tree file in Newick format. The leaf node labels should
        be the same as as row names in the heatmap file. E.g. row1, row2.

    output_file: str, optional
        If specified the heat-tree will be rendered in that file as a PNG image,
        otherwise interactive browser will pop-up. **N.B.** program will wait
        for you to exit the browser before continuing.
    '''

    t1 = Tree(tree_file)
    tss = TreeStyle()
    #t.populate(8)
    # Calculate the midpoint node
    R = t1.get_midpoint_outgroup()
    # and set it as tree outgroup
    t1.set_outgroup(R)  # To operate with numbers efficiently

    import matplotlib.cm as cm
    from matplotlib.colors import rgb2hex
    import matplotlib as mpl
    norm = mpl.colors.Normalize(vmin=0.8, vmax=1)  # map2count[map[0]][0]
    cmap_blue = cm.Blues
    m2 = cm.ScalarMappable(norm=norm, cmap=cmap_blue)

    leaf_number = 0
    for lf in t1.iter_leaves():
        leaf_number += 1
        lf.branch_vertical_margin = 0

        try:
            data = taxid2n[str(lf.name)]
        except:
            data = [0]

        try:
            st = taxid2st[lf.name]
        except:
            st = False
            '''
            if "taxon2accession_list" not in locals():
                from chlamdb.biosqldb import manipulate_biosqldb
                server, db = manipulate_biosqldb.load_db("k_cosson_05_16")
                sql = 'select taxon_id, accession from bioentry where biodatabase_id=104'
                data_tax = server.adaptor.execute_and_fetchall(sql,)
                taxon2accession_list = {}
                for i in data_tax:
                    if i[0] not in taxon2accession_list:
                        taxon2accession_list[i[0]] = [i[1]]
                    else:
                       taxon2accession_list[i[0]].append(i[1])
            else:
                for taxon in taxon2accession_list:
                    if lf.name in taxon2accession_list[taxon]:
                        for accession in taxon2accession_list[taxon]:
                            print lf.name, accession
                            try:
                                st = taxid2st[accession]
                                data = taxid2n[accession]
                                print 'st ok!!', st
                                break
                            except:
                                continue
             '''

        if accession2description:
            try:
                lf.name = accession2description[lf.name]
            except:
                pass
        if st:
            lf.name = lf.name + ' (' + st + ')'
        else:
            pass
        for col, value in enumerate(data):

            if leaf_number == 1:
                n = TextFace('%s' % (genes[col]), fsize=6)
                n.vt_align = 2
                n.hz_align = 2
                n.rotation = 270
                n.margin_top = 0
                n.margin_right = 0
                n.margin_left = 4
                n.margin_bottom = 0
                n.inner_background.color = "white"
                n.opacity = 1.
                tss.aligned_header.add_face(n, col)
                #lf.add_face(n, col, position="aligned")

            if value > 0:
                n = TextFace('  ')
                n.margin_top = 0
                n.margin_right = 0
                n.margin_left = 0
                n.margin_bottom = 0
                n.inner_background.color = rgb2hex(m2.to_rgba(
                    float(value)))  #'#140718' #"#81BEF7"
                n.opacity = 1.
                lf.add_face(n, col, position="aligned")

            else:
                n = TextFace('  ')
                n.margin_top = 0
                n.margin_right = 0
                n.margin_left = 0
                n.margin_bottom = 0
                n.inner_background.color = "white"
                n.opacity = 1.
                lf.add_face(n, col, position="aligned")

    return t1, leaf_number, tss
Example #14
0
def plot_heatmap_tree_locus(biodb,
                            tree_file,
                            taxid2count,
                            taxid2identity=False,
                            taxid2locus=False,
                            reference_taxon=False,
                            n_paralogs_barplot=False):
    '''

    plot tree and associated heatmap with count of homolgs
    optional:
        - add identity of closest homolog
        - add locus tag of closest homolog

    '''

    from chlamdb.biosqldb import manipulate_biosqldb

    server, db = manipulate_biosqldb.load_db(biodb)

    taxid2organism = manipulate_biosqldb.taxon_id2genome_description(
        server, biodb, True)

    t1 = Tree(tree_file)
    ts = TreeStyle()
    ts.draw_guiding_lines = True
    ts.guiding_lines_color = "gray"
    # Calculate the midpoint node
    R = t1.get_midpoint_outgroup()
    # and set it as tree outgroup
    t1.set_outgroup(R)

    leaf_number = 0

    for lf in t1.iter_leaves():

        if str(lf.name) not in taxid2count:
            taxid2count[str(lf.name)] = 0

    max_count = max([taxid2count[str(lf.name)] for lf in t1.iter_leaves()])

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

        # top leaf, add header
        if i == 0:

            n = TextFace('Number of homologs')
            n.margin_top = 1
            n.margin_right = 1
            n.margin_left = 20
            n.margin_bottom = 1
            n.inner_background.color = "white"
            n.opacity = 1.
            n.rotation = -25
            #lf.add_face(n, 7, position="aligned")
            ts.aligned_header.add_face(n, 1)

            if taxid2identity:
                n = TextFace('Protein identity')
                n.margin_top = 1
                n.margin_right = 1
                n.margin_left = 20
                n.margin_bottom = 1
                n.inner_background.color = "white"
                n.opacity = 1.
                n.rotation = -25
                #lf.add_face(n, 7, position="aligned")
                ts.aligned_header.add_face(n, 2)
            if taxid2locus:
                n = TextFace('Locus tag')
                n.margin_top = 1
                n.margin_right = 1
                n.margin_left = 20
                n.margin_bottom = 1
                n.inner_background.color = "white"
                n.opacity = 1.
                n.rotation = -25
                #lf.add_face(n, 7, position="aligned")
                ts.aligned_header.add_face(n, 3)

        leaf_number += 1

        lf.branch_vertical_margin = 0

        data = [taxid2count[str(lf.name)]]

        # possibility to add one or more columns
        for col, value in enumerate(data):
            col_index = col
            if value > 0:
                n = TextFace(' %s ' % str(value))
                n.margin_top = 2

                n.margin_right = 2
                if col == 0:
                    n.margin_left = 20
                else:
                    n.margin_left = 2
                n.margin_bottom = 2
                n.inner_background.color = "white"  # #81BEF7
                n.opacity = 1.
                lf.add_face(n, col, position="aligned")

            else:
                n = TextFace(' %s ' % str(value))
                n.margin_top = 2
                n.margin_right = 2
                if col == 0:
                    n.margin_left = 20
                else:
                    n.margin_left = 2
                n.margin_bottom = 2
                n.inner_background.color = "white"
                n.opacity = 1.
                lf.add_face(n, col, position="aligned")
        # optionally indicate number of paralogs as a barplot
        if n_paralogs_barplot:
            col_index += 1
            percent = (float(value) / max_count) * 100
            n = StackedBarFace([percent, 100 - percent],
                               width=150,
                               height=18,
                               colors=['#6699ff', 'white'],
                               line_color='white')
            n.rotation = 0
            n.inner_border.color = "white"
            n.inner_border.width = 0
            n.margin_right = 15
            n.margin_left = 0
            lf.add_face(n, col + 1, position="aligned")

        # optionally add additionnal column with identity
        if taxid2identity:
            import matplotlib.cm as cm
            from matplotlib.colors import rgb2hex
            import matplotlib as mpl

            norm = mpl.colors.Normalize(vmin=0, vmax=100)
            cmap = cm.OrRd
            m = cm.ScalarMappable(norm=norm, cmap=cmap)

            try:
                if round(taxid2identity[str(lf.name)], 2) != 100:
                    value = "%.2f" % round(taxid2identity[str(lf.name)], 2)
                else:
                    value = "%.1f" % round(taxid2identity[str(lf.name)], 2)
            except:
                value = '-'
            if str(lf.name) == str(reference_taxon):
                value = '         '
            n = TextFace(' %s ' % value)
            n.margin_top = 2
            n.margin_right = 2
            n.margin_left = 20
            n.margin_bottom = 2
            if not value.isspace() and value is not '-':
                n.inner_background.color = rgb2hex(m.to_rgba(float(value)))
                if float(value) > 82:
                    n.fgcolor = 'white'
            n.opacity = 1.
            if str(lf.name) == str(reference_taxon):
                n.inner_background.color = '#800000'

            lf.add_face(n, col_index + 1, position="aligned")
        # optionaly add column with locus name
        if taxid2locus:
            try:
                value = str(taxid2locus[str(lf.name)])
            except:
                value = '-'
            n = TextFace(' %s ' % value)
            n.margin_top = 2
            n.margin_right = 2
            n.margin_left = 2
            n.margin_bottom = 2
            if str(lf.name) != str(reference_taxon):
                n.inner_background.color = "white"
            else:
                n.fgcolor = '#ff0000'
                n.inner_background.color = "white"
            n.opacity = 1.
            lf.add_face(n, col_index + 2, position="aligned")
        lf.name = taxid2organism[str(lf.name)]

    return t1, leaf_number, ts
Example #15
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
Example #16
0
def run(args):
    if args.text_mode:
        from ete3 import Tree
        for tindex, tfile in enumerate(args.src_tree_iterator):
            #print tfile
            if args.raxml:
                nw = re.sub(":(\d+\.\d+)\[(\d+)\]", ":\\1[&&NHX:support=\\2]",
                            open(tfile).read())
                t = Tree(nw)
            else:
                t = Tree(tfile)

            print(
                t.get_ascii(show_internal=args.show_internal_names,
                            attributes=args.show_attributes))
        return

    import random
    import re
    import colorsys
    from collections import defaultdict
    from ete3 import (Tree, PhyloTree, TextFace, RectFace, faces, TreeStyle,
                      add_face_to_node, random_color)

    global FACES

    if args.face:
        FACES = parse_faces(args.face)
    else:
        FACES = []

    # VISUALIZATION
    ts = TreeStyle()
    ts.mode = args.mode
    ts.show_leaf_name = True
    ts.tree_width = args.tree_width

    for f in FACES:
        if f["value"] == "@name":
            ts.show_leaf_name = False
            break

    if args.as_ncbi:
        ts.show_leaf_name = False
        FACES.extend(
            parse_faces([
                'value:@sci_name, size:10, fstyle:italic',
                'value:@taxid, color:grey, size:6, format:" - %s"',
                'value:@sci_name, color:steelblue, size:7, pos:b-top, nodetype:internal',
                'value:@rank, color:indianred, size:6, pos:b-bottom, nodetype:internal',
            ]))

    if args.alg:
        FACES.extend(
            parse_faces([
                'value:@sequence, size:10, pos:aligned, ftype:%s' %
                args.alg_type
            ]))

    if args.heatmap:
        FACES.extend(
            parse_faces(['value:@name, size:10, pos:aligned, ftype:heatmap']))

    if args.bubbles:
        for bubble in args.bubbles:
            FACES.extend(
                parse_faces([
                    'value:@%s, pos:float, ftype:bubble, opacity:0.4' % bubble,
                ]))

    ts.branch_vertical_margin = args.branch_separation
    if args.show_support:
        ts.show_branch_support = True
    if args.show_branch_length:
        ts.show_branch_length = True
    if args.force_topology:
        ts.force_topology = True
    ts.layout_fn = lambda x: None

    for tindex, tfile in enumerate(args.src_tree_iterator):
        #print tfile
        if args.raxml:
            nw = re.sub(":(\d+\.\d+)\[(\d+)\]", ":\\1[&&NHX:support=\\2]",
                        open(tfile).read())
            t = PhyloTree(nw)
        else:
            t = PhyloTree(tfile)

        if args.alg:
            t.link_to_alignment(args.alg, alg_format=args.alg_format)

        if args.heatmap:
            DEFAULT_COLOR_SATURATION = 0.3
            BASE_LIGHTNESS = 0.7

            def gradient_color(value, max_value, saturation=0.5, hue=0.1):
                def rgb2hex(rgb):
                    return '#%02x%02x%02x' % rgb

                def hls2hex(h, l, s):
                    return rgb2hex(
                        tuple([
                            int(x * 255) for x in colorsys.hls_to_rgb(h, l, s)
                        ]))

                lightness = 1 - (value * BASE_LIGHTNESS) / max_value
                return hls2hex(hue, lightness, DEFAULT_COLOR_SATURATION)

            heatmap_data = {}
            max_value, min_value = None, None
            for line in open(args.heatmap):
                if line.startswith('#COLNAMES'):
                    pass
                elif line.startswith('#') or not line.strip():
                    pass
                else:
                    fields = line.split('\t')
                    name = fields[0].strip()

                    values = [float(x) if x else None for x in fields[1:]]

                    maxv = max(values)
                    minv = min(values)
                    if max_value is None or maxv > max_value:
                        max_value = maxv
                    if min_value is None or minv < min_value:
                        min_value = minv
                    heatmap_data[name] = values

            heatmap_center_value = 0
            heatmap_color_center = "white"
            heatmap_color_up = 0.3
            heatmap_color_down = 0.7
            heatmap_color_missing = "black"

            heatmap_max_value = abs(heatmap_center_value - max_value)
            heatmap_min_value = abs(heatmap_center_value - min_value)

            if heatmap_center_value <= min_value:
                heatmap_max_value = heatmap_min_value + heatmap_max_value
            else:
                heatmap_max_value = max(heatmap_min_value, heatmap_max_value)

        # scale the tree
        if not args.height:
            args.height = None
        if not args.width:
            args.width = None

        f2color = {}
        f2last_seed = {}
        for node in t.traverse():
            node.img_style['size'] = 0
            if len(node.children) == 1:
                node.img_style['size'] = 2
                node.img_style['shape'] = "square"
                node.img_style['fgcolor'] = "steelblue"

            ftype_pos = defaultdict(int)

            for findex, f in enumerate(FACES):
                if (f['nodetype'] == 'any'
                        or (f['nodetype'] == 'leaf' and node.is_leaf()) or
                    (f['nodetype'] == 'internal' and not node.is_leaf())):

                    # if node passes face filters
                    if node_matcher(node, f["filters"]):
                        if f["value"].startswith("@"):
                            fvalue = getattr(node, f["value"][1:], None)
                        else:
                            fvalue = f["value"]

                        # if node's attribute has content, generate face
                        if fvalue is not None:
                            fsize = f["size"]
                            fbgcolor = f["bgcolor"]
                            fcolor = f['color']

                            if fcolor:
                                # Parse color options
                                auto_m = re.search("auto\(([^)]*)\)", fcolor)
                                if auto_m:
                                    target_attr = auto_m.groups()[0].strip()
                                    if not target_attr:
                                        color_keyattr = f["value"]
                                    else:
                                        color_keyattr = target_attr

                                    color_keyattr = color_keyattr.lstrip('@')
                                    color_bin = getattr(
                                        node, color_keyattr, None)

                                    last_seed = f2last_seed.setdefault(
                                        color_keyattr, random.random())

                                    seed = last_seed + 0.10 + random.uniform(
                                        0.1, 0.2)
                                    f2last_seed[color_keyattr] = seed

                                    fcolor = f2color.setdefault(
                                        color_bin, random_color(h=seed))

                            if fbgcolor:
                                # Parse color options
                                auto_m = re.search("auto\(([^)]*)\)", fbgcolor)
                                if auto_m:
                                    target_attr = auto_m.groups()[0].strip()
                                    if not target_attr:
                                        color_keyattr = f["value"]
                                    else:
                                        color_keyattr = target_attr

                                    color_keyattr = color_keyattr.lstrip('@')
                                    color_bin = getattr(
                                        node, color_keyattr, None)

                                    last_seed = f2last_seed.setdefault(
                                        color_keyattr, random.random())

                                    seed = last_seed + 0.10 + random.uniform(
                                        0.1, 0.2)
                                    f2last_seed[color_keyattr] = seed

                                    fbgcolor = f2color.setdefault(
                                        color_bin, random_color(h=seed))

                            if f["ftype"] == "text":
                                if f.get("format", None):
                                    fvalue = f["format"] % fvalue

                                F = TextFace(fvalue,
                                             fsize=fsize,
                                             fgcolor=fcolor or "black",
                                             fstyle=f.get('fstyle', None))

                            elif f["ftype"] == "fullseq":
                                F = faces.SeqMotifFace(seq=fvalue,
                                                       seq_format="seq",
                                                       seqtail_format="seq",
                                                       height=fsize)
                            elif f["ftype"] == "compactseq":
                                F = faces.SeqMotifFace(
                                    seq=fvalue,
                                    seq_format="compactseq",
                                    seqtail_format="compactseq",
                                    height=fsize)
                            elif f["ftype"] == "blockseq":
                                F = faces.SeqMotifFace(
                                    seq=fvalue,
                                    seq_format="blockseq",
                                    seqtail_format="blockseq",
                                    height=fsize,
                                    fgcolor=fcolor or "slategrey",
                                    bgcolor=fbgcolor or "slategrey",
                                    scale_factor=1.0)
                                fbgcolor = None
                            elif f["ftype"] == "bubble":
                                try:
                                    v = float(fvalue)
                                except ValueError:
                                    rad = fsize
                                else:
                                    rad = fsize * v
                                F = faces.CircleFace(radius=rad,
                                                     style="sphere",
                                                     color=fcolor
                                                     or "steelblue")

                            elif f["ftype"] == "heatmap":
                                if not f['column']:
                                    col = ftype_pos[f["pos"]]
                                else:
                                    col = f["column"]

                                for i, value in enumerate(
                                        heatmap_data.get(node.name, [])):
                                    ftype_pos[f["pos"]] += 1

                                    if value is None:
                                        color = heatmap_color_missing
                                    elif value > heatmap_center_value:
                                        color = gradient_color(
                                            abs(heatmap_center_value - value),
                                            heatmap_max_value,
                                            hue=heatmap_color_up)
                                    elif value < heatmap_center_value:
                                        color = gradient_color(
                                            abs(heatmap_center_value - value),
                                            heatmap_max_value,
                                            hue=heatmap_color_down)
                                    else:
                                        color = heatmap_color_center
                                    node.add_face(RectFace(
                                        20, 20, color, color),
                                                  position="aligned",
                                                  column=col + i)
                                    # Add header
                                    # for i, name in enumerate(header):
                                    #    nameF = TextFace(name, fsize=7)
                                    #    nameF.rotation = -90
                                    #    tree_style.aligned_header.add_face(nameF, column=i)
                                F = None

                            elif f["ftype"] == "profile":
                                # internal profiles?
                                F = None
                            elif f["ftype"] == "barchart":
                                F = None
                            elif f["ftype"] == "piechart":
                                F = None

                            # Add the Face
                            if F:
                                F.opacity = f['opacity'] or 1.0

                                # Set face general attributes
                                if fbgcolor:
                                    F.background.color = fbgcolor

                                if not f['column']:
                                    col = ftype_pos[f["pos"]]
                                    ftype_pos[f["pos"]] += 1
                                else:
                                    col = f["column"]
                                node.add_face(F, column=col, position=f["pos"])

        if args.image:
            t.render("t%d.%s" % (tindex, args.image),
                     tree_style=ts,
                     w=args.width,
                     h=args.height,
                     units=args.size_units)
        else:
            t.show(None, tree_style=ts)
Example #17
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")
Example #18
0
def run(args):
    if args.text_mode:
        from ete3 import Tree
        for tindex, tfile in enumerate(args.src_tree_iterator):
            #print tfile
            if args.raxml:
                nw = re.sub(":(\d+\.\d+)\[(\d+)\]", ":\\1[&&NHX:support=\\2]", open(tfile).read())
                t = Tree(nw)
            else:
                t = Tree(tfile)

            print(t.get_ascii(show_internal=args.show_internal_names,
                              attributes=args.show_attributes))
        return

    import random
    import re
    import colorsys
    from collections import defaultdict
    from ete3 import (Tree, PhyloTree, TextFace, RectFace, faces, TreeStyle,
                         add_face_to_node, random_color)

    global FACES

    if args.face:
        FACES = parse_faces(args.face)
    else:
        FACES = []

    # VISUALIZATION
    ts = TreeStyle()
    ts.mode = args.mode
    ts.show_leaf_name = True
    ts.tree_width = args.tree_width


    for f in FACES:
        if f["value"] == "@name":
            ts.show_leaf_name = False
            break

    if args.as_ncbi:
        ts.show_leaf_name = False
        FACES.extend(parse_faces(
            ['value:@sci_name, size:10, fstyle:italic',
             'value:@taxid, color:grey, size:6, format:" - %s"',
             'value:@sci_name, color:steelblue, size:7, pos:b-top, nodetype:internal',
             'value:@rank, color:indianred, size:6, pos:b-bottom, nodetype:internal',
         ]))


    if args.alg:
        FACES.extend(parse_faces(
            ['value:@sequence, size:10, pos:aligned, ftype:%s' %args.alg_type]
         ))

    if args.heatmap:
        FACES.extend(parse_faces(
            ['value:@name, size:10, pos:aligned, ftype:heatmap']
         ))

    if args.bubbles:
        for bubble in args.bubbles:
            FACES.extend(parse_faces(
                ['value:@%s, pos:float, ftype:bubble, opacity:0.4' %bubble,
             ]))

    ts.branch_vertical_margin = args.branch_separation
    if args.show_support:
        ts.show_branch_support = True
    if args.show_branch_length:
        ts.show_branch_length = True
    if args.force_topology:
        ts.force_topology = True
    ts.layout_fn = lambda x: None

    for tindex, tfile in enumerate(args.src_tree_iterator):
        #print tfile
        if args.raxml:
            nw = re.sub(":(\d+\.\d+)\[(\d+)\]", ":\\1[&&NHX:support=\\2]", open(tfile).read())
            t = PhyloTree(nw)
        else:
            t = PhyloTree(tfile)


        if args.alg:
            t.link_to_alignment(args.alg, alg_format=args.alg_format)

        if args.heatmap:
            DEFAULT_COLOR_SATURATION = 0.3
            BASE_LIGHTNESS = 0.7
            def gradient_color(value, max_value, saturation=0.5, hue=0.1):
                def rgb2hex(rgb):
                    return '#%02x%02x%02x' % rgb
                def hls2hex(h, l, s):
                    return rgb2hex( tuple([int(x*255) for x in colorsys.hls_to_rgb(h, l, s)]))

                lightness = 1 - (value * BASE_LIGHTNESS) / max_value
                return hls2hex(hue, lightness, DEFAULT_COLOR_SATURATION)


            heatmap_data = {}
            max_value, min_value = None, None
            for line in open(args.heatmap):
                if line.startswith('#COLNAMES'):
                    pass
                elif line.startswith('#') or not line.strip():
                    pass
                else:
                    fields = line.split('\t')
                    name = fields[0].strip()

                    values = [float(x) if x else None for x in fields[1:]]

                    maxv = max(values)
                    minv = min(values)
                    if max_value is None or maxv > max_value:
                        max_value = maxv
                    if min_value is None or minv < min_value:
                        min_value = minv
                    heatmap_data[name] = values

            heatmap_center_value = 0
            heatmap_color_center = "white"
            heatmap_color_up = 0.3
            heatmap_color_down = 0.7
            heatmap_color_missing = "black"

            heatmap_max_value = abs(heatmap_center_value - max_value)
            heatmap_min_value = abs(heatmap_center_value - min_value)

            if heatmap_center_value <= min_value:
                heatmap_max_value = heatmap_min_value + heatmap_max_value
            else:
                heatmap_max_value = max(heatmap_min_value, heatmap_max_value)



        # scale the tree
        if not args.height:
            args.height = None
        if not args.width:
            args.width = None

        f2color = {}
        f2last_seed = {}
        for node in t.traverse():
            node.img_style['size'] = 0
            if len(node.children) == 1:
                node.img_style['size'] = 2
                node.img_style['shape'] = "square"
                node.img_style['fgcolor'] = "steelblue"

            ftype_pos = defaultdict(int)

            for findex, f in enumerate(FACES):
                if (f['nodetype'] == 'any' or
                    (f['nodetype'] == 'leaf' and node.is_leaf()) or
                    (f['nodetype'] == 'internal' and not node.is_leaf())):


                    # if node passes face filters
                    if node_matcher(node, f["filters"]):
                        if f["value"].startswith("@"):
                            fvalue = getattr(node, f["value"][1:], None)
                        else:
                            fvalue = f["value"]

                        # if node's attribute has content, generate face
                        if fvalue is not None:
                            fsize = f["size"]
                            fbgcolor = f["bgcolor"]
                            fcolor = f['color']

                            if fcolor:
                                # Parse color options
                                auto_m = re.search("auto\(([^)]*)\)", fcolor)
                                if auto_m:
                                    target_attr = auto_m.groups()[0].strip()
                                    if not target_attr :
                                        color_keyattr = f["value"]
                                    else:
                                        color_keyattr = target_attr

                                    color_keyattr = color_keyattr.lstrip('@')
                                    color_bin = getattr(node, color_keyattr, None)

                                    last_seed = f2last_seed.setdefault(color_keyattr, random.random())

                                    seed = last_seed + 0.10 + random.uniform(0.1, 0.2)
                                    f2last_seed[color_keyattr] = seed

                                    fcolor = f2color.setdefault(color_bin, random_color(h=seed))

                            if fbgcolor:
                                # Parse color options
                                auto_m = re.search("auto\(([^)]*)\)", fbgcolor)
                                if auto_m:
                                    target_attr = auto_m.groups()[0].strip()
                                    if not target_attr :
                                        color_keyattr = f["value"]
                                    else:
                                        color_keyattr = target_attr

                                    color_keyattr = color_keyattr.lstrip('@')
                                    color_bin = getattr(node, color_keyattr, None)

                                    last_seed = f2last_seed.setdefault(color_keyattr, random.random())

                                    seed = last_seed + 0.10 + random.uniform(0.1, 0.2)
                                    f2last_seed[color_keyattr] = seed

                                    fbgcolor = f2color.setdefault(color_bin, random_color(h=seed))

                            if f["ftype"] == "text":
                                if f.get("format", None):
                                    fvalue = f["format"] % fvalue

                                F = TextFace(fvalue,
                                             fsize = fsize,
                                             fgcolor = fcolor or "black",
                                             fstyle = f.get('fstyle', None))

                            elif f["ftype"] == "fullseq":
                                F = faces.SeqMotifFace(seq=fvalue, seq_format="seq",
                                                       seqtail_format="seq",
                                                       height=fsize)
                            elif f["ftype"] == "compactseq":
                                F = faces.SeqMotifFace(seq=fvalue, seq_format="compactseq",
                                                       seqtail_format="compactseq",
                                                       height=fsize)
                            elif f["ftype"] == "blockseq":
                                F = faces.SeqMotifFace(seq=fvalue, seq_format="blockseq",
                                                   seqtail_format="blockseq",
                                                       height=fsize,
                                                       fgcolor=fcolor or "slategrey",
                                                       bgcolor=fbgcolor or "slategrey",
                                                       scale_factor = 1.0)
                                fbgcolor = None
                            elif f["ftype"] == "bubble":
                                try:
                                    v = float(fvalue)
                                except ValueError:
                                    rad = fsize
                                else:
                                    rad = fsize * v
                                F = faces.CircleFace(radius=rad, style="sphere",
                                                     color=fcolor or "steelblue")

                            elif f["ftype"] == "heatmap":
                                if not f['column']:
                                    col = ftype_pos[f["pos"]]
                                else:
                                    col = f["column"]

                                for i, value in enumerate(heatmap_data.get(node.name, [])):
                                    ftype_pos[f["pos"]] += 1

                                    if value is None:
                                        color = heatmap_color_missing
                                    elif value > heatmap_center_value:
                                        color = gradient_color(abs(heatmap_center_value - value), heatmap_max_value, hue=heatmap_color_up)
                                    elif value < heatmap_center_value:
                                        color = gradient_color(abs(heatmap_center_value - value), heatmap_max_value, hue=heatmap_color_down)
                                    else:
                                        color = heatmap_color_center
                                    node.add_face(RectFace(20, 20, color, color), position="aligned", column=col + i)
                                    # Add header
                                    # for i, name in enumerate(header):
                                    #    nameF = TextFace(name, fsize=7)
                                    #    nameF.rotation = -90
                                    #    tree_style.aligned_header.add_face(nameF, column=i)
                                F = None

                            elif f["ftype"] == "profile":
                                # internal profiles?
                                F = None
                            elif f["ftype"] == "barchart":
                                F = None
                            elif f["ftype"] == "piechart":
                                F = None



                            # Add the Face
                            if F:
                                F.opacity = f['opacity'] or 1.0

                                # Set face general attributes
                                if fbgcolor:
                                    F.background.color = fbgcolor

                                if not f['column']:
                                    col = ftype_pos[f["pos"]]
                                    ftype_pos[f["pos"]] += 1
                                else:
                                    col = f["column"]
                                node.add_face(F, column=col, position=f["pos"])

        if args.image:
            t.render("t%d.%s" %(tindex, args.image),
                     tree_style=ts, w=args.width, h=args.height, units=args.size_units)
        else:
            t.show(None, tree_style=ts)