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

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

            habitat_face = TextFace(tip2info[node.name][2], fsize=11, fgcolor='white')
            habitat_face.background.color = 'steelblue'
            habitat_face.margin_left = 3
            habitat_face.margin_top = 3
            habitat_face.margin_right = 3
            habitat_face.margin_bottom = 3
            add_face_to_node(habitat_face, node, column=3, position='aligned')
    else:
        node.img_style['size'] = 4
        node.img_style['shape'] = 'square'
        if node.name:
            name_face = TextFace(node.name, fgcolor='grey', fsize=10)
            name_face.margin_bottom = 2
            add_face_to_node(name_face, node, column=0, position='branch-top')
        if node.support:
            support_face = TextFace(node.support, fgcolor='indianred', fsize=10)
            add_face_to_node(support_face, node, column=0, position='branch-bottom')
Example #2
0
def custom_layout(node):
    if node.is_leaf():
        aligned_name_face = TextFace(node.name, fgcolor='olive', fsize=14)
        add_face_to_node(aligned_name_face, node, column=2, position='aligned')
        name_face = TextFace(node.name, fgcolor='#333333', fsize=11)
        add_face_to_node(name_face, node, column=2, position='branch-right')
        node.img_style['size'] = 0

        if node.name in tip2info:
            # For some reason img urls are very slow!
            #img_face = ImgFace(tip2info[node.name][0], is_url=True)
            #add_face_to_node(img_face, node, column=4, position='branch-right')
            habitat_face = TextFace(tip2info[node.name][2], fsize=11, fgcolor='white')
            habitat_face.background.color = 'steelblue'
            habitat_face.margin_left = 3
            habitat_face.margin_top = 3
            habitat_face.margin_right = 3
            habitat_face.margin_bottom = 3
            add_face_to_node(habitat_face, node, column=3, position='aligned')
    else:
        node.img_style['size'] = 4
        node.img_style['shape'] = 'square'
        if node.name:
            name_face = TextFace(node.name, fgcolor='grey', fsize=10)
            name_face.margin_bottom = 2
            add_face_to_node(name_face, node, column=0, position='branch-top')
        if node.support:
            support_face = TextFace(node.support, fgcolor='indianred', fsize=10)
            add_face_to_node(support_face, node, column=0, position='branch-bottom')
Example #3
0
    def ly_tax_labels(node):
        if node.is_leaf():
            c = LABEL_START_COL
            largest = 0
            for tname in TRACKED_CLADES:
                if hasattr(node, "named_lineage") and tname in node.named_lineage:
                    linF = TextFace(tname, fsize=10, fgcolor='white')
                    linF.margin_left = 3
                    linF.margin_right = 2
                    linF.background.color = lin2color[tname]
                    add_face_to_node(linF, node, c, position='aligned')
                    c += 1

            for n in range(c, len(TRACKED_CLADES)):
                add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'), node, c, position='aligned')
                c+=1
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 #5
0
ts = TreeStyle()
ts.show_leaf_name = False
ts.show_branch_length = False
ts.show_branch_support = True

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

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

mark2 = TextFace("X", fsize=10, fgcolor="black")
# Set some attributes
mark2.margin_top = 0
mark2.margin_right = 1
mark2.margin_left = 1
mark2.margin_bottom = 0
mark2.opacity = 1  # from 0 to 1
mark2.border.width = 1
mark2.background.color = "#F5F5DC"
ts.legend.add_face(mark2, column=0)

mark3 = TextFace("Selected branches", fsize=10, fgcolor="black")
    def custom_layout(self,node):
        if node.is_leaf():
           aligned_name_face = TextFace(node.name, fgcolor='olive', fsize=12)
           aligned_name_face.margin_top = 5
           aligned_name_face.margin_right = 5
           aligned_name_face.margin_left = 5
           aligned_name_face.margin_bottom = 5
           aligned_name_face.hz_align = 0     #0 = left, 1 = center, 2 = right 
           add_face_to_node(aligned_name_face, node, column=2, position='aligned')
           #name_face = TextFace(node.name, fgcolor='#333333', fsize=11)
           #name_face.margin_top = 3
           #name_face.margin_right = 3
           #name_face.margin_left = 3
           #name_face.margin_bottom = 3 
           #add_face_to_node(name_face, node, column=2, position='branch-right')
           node.img_style['size'] = 0
           #---------------------------------------------
           #displaying extra categorical and numeric data
           if (node.name in self._tip2info):
              column_no = 3
              for headerIndex, dataheader in enumerate(self._tip2headers):
                  extra_data = self._tip2info[node.name][headerIndex]
                  if isinstance( extra_data, ( int, float ) ):
                     extra_face = BarChartFace([extra_data], width=100,height=25,colors=[self._tip2color[node.name][headerIndex]],labels=[dataheader],min_value=0.0,max_value=self._tip_max)
                  else:
                     extra_face = TextFace(extra_data, fsize=11, fgcolor='black')
                     extra_face.background.color = self._tip2color[node.name][headerIndex]

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

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

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

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

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

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

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

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

    #Read in tree and assign additional information to each leaf
    t = Tree(context_file[0].replace('.fna', '.unique.tree'))

    for node in t.traverse():
        if node.is_leaf():
            id = node.name.split('__')[1]
            node.add_features(organism=gene_dict[id]['organism'])
            node.add_features(assembly=gene_dict[id]['assembly'])
            node.add_features(pident=gene_dict[id]['perc_id'])
            if args.compressed == True:
                node.add_features(cluster_size=gene_dict[id]['cluster_size'])

    #Create dictionary to append motifs to
    motif_dict = {}

    #Create keyword lists to set gene color
    tnps = [
        'iscr',
        'transpos',
        'tnp',
        'insertion',
        '-like',
    ]
    ints = ['inti', 'integrase', 'xerc', 'xerd']
    mobiles=['secretion', 'mobiliza', 'moba', 'mobb', 'mobc', 'mobl', 'plasmid', 'relaxase',\
     'conjugation', 'type iv']
    res = [
        'lactam', 'aminoglyco', 'fluoroquinolo', 'tetracyclin', 'macrolid',
        'carbapenem'
    ]

    print('decorating the tree...')
    #Create motifs for each gene associated with a leaf
    for leaf in t.traverse():
        if leaf.is_leaf():

            #traverse through environment genes for the respective sequence
            for key, value in gene_dict.items():
                motifs = []

                #Assign start and end position for annotated gene
                gene_start = gene_dict[key]['start']
                gene_end = gene_dict[key]['stop']

                #Sort such that the greater number is end and smaller is start
                if gene_start > gene_end:

                    gene_end = gene_dict[key]['start']
                    gene_start = gene_dict[key]['stop']

                #Append motif for annotated gene
                gene_motif=[gene_start, gene_end,'()', \
                2, 10, 'red', 'red', 'arial|10|black|'+str(gene_dict[key]['name'])]

                if not str(gene_dict[key]['frame']).startswith('-'):
                    ori_motif = [
                        gene_end, gene_end + 10, '>', 2, 10, 'red', 'red', None
                    ]

                else:
                    ori_motif=[gene_start-10, gene_start, '<', 2, 10, \
                    'red', 'red', None]

                motifs.extend([gene_motif, ori_motif])

                for key2, value2 in value['env_genes'].items():

                    #Set color, default is orange
                    color = 'orange'

                    if any(keyword in value2['env_name'].lower()
                           for keyword in tnps):
                        color = 'violet'
                    if any(keyword in value2['env_name'].lower()
                           for keyword in ints):
                        color = 'yellow'
                    if any(keyword in value2['env_name'].lower()
                           for keyword in mobiles):
                        color = 'green'
                    if any(keyword in value2['env_name'].lower()
                           for keyword in res):
                        color = 'red'
                    if 'hypothetical' in value2['env_name']:
                        color = 'grey'

                    #Create motif for one env gene at a time and append to motif list
                    motif=[value2['env_start'], value2['env_stop'], '()', 2, 10, color, color, \
                    'arial|10|black|'+str(value2['env_name'])]

                    #Set condition: If env gene != annotated gene, append motif
                    arg_pos = {
                        i
                        for i in range(int(gene_motif[0]), int(gene_motif[1]))
                    }
                    env_pos = {i for i in range(int(motif[0]), int(motif[1]))}

                    #Calculate overlap percentage between annotated gene and env gene
                    total_overlap = float(len(arg_pos.intersection(env_pos)))
                    overlap_perc = float(
                        total_overlap / int(gene_dict[key]['length'])) * 100

                    if overlap_perc <= 70.0:
                        motifs.append(motif)

                        #Create additional motif to show gene orientation
                        if value2['env_strand'] == '+':
                            ori_motif=[value2['env_stop'], value2['env_stop']+10, '>', 2, 10, \
                            color, color, None]

                        else:
                            ori_motif=[value2['env_start']-10, value2['env_start'], '<', 2, 10, \
                            color, color, None]

                        motifs.append(ori_motif)

                #append motif lists to respective annotated gene in dict
                gene_dict[key]['motifs'] = motifs

    #Set node style
    nst_plasmid = NodeStyle()
    nst_plasmid['bgcolor'] = 'DarkSeaGreen'
    nst_other = NodeStyle()
    nst_other = 'AntiqueWhite'

    #Now annotate the tree with the motifs
    for node in t.traverse():
        if node.is_leaf():
            if 'plasmid' in node.organism:
                node.set_style(nst_plasmid)
            else:
                node.set_style(nst_other)

            seqFace=SeqMotifFace(seq=None, motifs=gene_dict[node.name.split('__')[1]]['motifs'], \
            seq_format='blank', gap_format='line')
            (t & node.name).add_face(seqFace, 1, 'aligned')

            #Create box showing gene percent id
            similarity = TextFace(node.pident, fsize=8)
            similarity.margin_top = 2
            similarity.margin_bottom = 2
            similarity.margin_left = 2
            similarity.margin_right = 2

            #Set box background color based on pident
            if node.pident <= 90.0:
                similarity.background.color = 'DarkGoldenrod'
            elif 90.0 < node.pident <= 95.0:
                similarity.background.color = 'ForestGreen'
            elif 95.0 <= node.pident:
                similarity.background.color = 'YellowGreen'

            node.add_face(similarity, column=2, position='aligned')

            #Create box showing cluster size
            if args.compressed == True:
                clust_box = TextFace(node.cluster_size, fsize=8)
                clust_box.margin_top = 2
                clust_box.margin_bottom = 2
                clust_box.margin_left = 2
                clust_box.margin_right = 2

                node.add_face(clust_box, column=3, position='aligned')

    #Return the annotated tree
    return t
Example #8
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 #9
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