def my_layout(node):
    if node.is_leaf() and any(
        [node.name == x.split(';')[-1] for x in selection['Taxonomy_UniEuk']]):
        F = ete3.TextFace(node.name, tight_text=True, fgcolor='red')
    else:
        F = ete3.TextFace(node.name, tight_text=True)
    ete3.add_face_to_node(F, node, column=0, position="branch-right")
Example #2
0
def buildTreeDepthFirst(expr,parentList):
    if expr[0]==';':
        leafNodes=buildTreeDepthFirst(expr[1],parentList)
        treeNodes=buildTreeDepthFirst(expr[2],leafNodes)
        return treeNodes
    elif expr[0]=='if-then:':
        leafNodes1=buildTreeDepthFirst(expr[1],parentList)
        successLeafs=buildTreeDepthFirst(expr[2],leafNodes1)
        failActionNodes=[]
        for parent in parentList:
            c=parent.add_child(name='Failed Action')
            c.add_face(ete3.TextFace('Failed Action'),column=0)
            failActionNodes.append(c)
        failLeafs=buildTreeDepthFirst(expr[3],failActionNodes)
        return successLeafs+failLeafs
    #elif expr[0].find("Loop-for-") != -1:
    #    line=expr[0][9:]
    #    pos=line.find('-')
    #    loopCount=line[:pos]
    #    loopCount=int(loopCount)
    #    tree1=buildTreeDepthFirst(expr[1],parent)
    #    treeList=[tree1 for i in xrange(loopCount)]
    #    j=loopCount-2
    #    currentTree=treeList[loopCount-1]
    #    while j > -1:
    #        currentTree=appendTrees(treeList[j],currentTree)
    #        j=j-1
    #    return currentTree
    else:
        resultList=[]
        for parent in parentList:
            c=parent.add_child(name=expr)
            c.add_face(ete3.TextFace(expr),column=0)
            resultList.append(c)
        return resultList
Example #3
0
def displaySVsOnPhylogenies(pid, t, SVs):
    if pid in SVs:
        for SV in SVs[pid]:
            SVname = SV
            text_face = ete3.TextFace(SVname, fsize=vertical_margins[pid] - 5)
            branches = getBranchOrBranchesFor(SVs[pid][SV], t, set(), set(),
                                              set())
            if len(branches) > 1:
                text_face = ete3.TextFace(SVname,
                                          fsize=vertical_margins[pid] - 5,
                                          fgcolor="orangered")
            for branch in branches:
                branch.add_face(text_face, column=1, position="branch-top")
            #Collect data for statistics
            samples = SVs[pid][SV]
            ntips = 0
            for tip in t:
                ntips += 1
            if not displayCombo and len(samples) > 1 and len(samples) < ntips:
                SV_out.write(str(pid))
                SV_out.write("\t" + SV)
                if len(branches) > 1:
                    SV_out.write("\tMiss")
                else:
                    SV_out.write("\tMatch")
                SV_out.write("\n")
    else:
        print("No", pid, "in SVs")
Example #4
0
def add_legend(tstyle, varname, all_vals, smap, info, start_column, add_missing=False, add_sign=None, reverse_log=False, n_entries=5, fsize=4, no_opacity=False):  # NOTE very similar to add_smap_legend() in plot_2d_scatter() in python/lbplotting.py
    if len(all_vals) == 0:
        return
    assert add_sign in [None, '-', '+']
    tstyle.legend.add_face(ete3.TextFace('   %s ' % varname, fsize=fsize), column=start_column)
    min_val, max_val = get_scale_min(args.lb_metric, all_vals), max(all_vals)
    if min_val == max_val:
        return
    max_diff = (max_val - min_val) / float(n_entries - 1)
    val_list = list(numpy.arange(min_val, max_val + utils.eps, max_diff))  # first value is exactly <min_val>, last value is exactly <max_val> (eps is to keep it from missing the last one)
    # if add_sign is not None and add_sign == '-':  # for negative changes, we have the cmap using abs() and want to legend order to correspond
    #     val_list = reversed(val_list)  # arg, this breaks something deep in the legend maker, not sure what
    key_list = [None for _ in val_list]
    if add_missing:
        val_list += [None]
        key_list += ['missing!']  # doesn't matter what the last one is as long as it isn't in <affyfo>
    for val, key in zip(val_list, key_list):
        tstyle.legend.add_face(ete3.TextFace('', fsize=fsize), column=start_column)
        if smap is None:
            sz = get_size(min_val, max_val, val)
            rface = ete3.RectFace(sz, sz, bgcolor=plotting.getgrey(), fgcolor=None)
        else:
            rface = ete3.RectFace(6, 6, bgcolor=plotting.get_smap_color(smap, info, key=key, val=val), fgcolor=None)
        if not no_opacity:
            rface.opacity = opacity
        tstyle.legend.add_face(rface, column=start_column + 1)
        fstr = '%.1f' if args.lb_metric == 'cons-dist-aa' else '%.2f'
        tstyle.legend.add_face(ete3.TextFace((('  %s'+fstr) % (add_sign if add_sign is not None else '', math.exp(val) if reverse_log else val)) if key is None else '  missing', fsize=fsize), column=start_column + 2)
Example #5
0
def displayDriversOnPhylogenies(pid, t, drivers, deletions):
    if pid in drivers:
        for gene in drivers[pid]:
            append = ""
            if len(drivers[pid][gene]) > 1:
                append = "_a"
            for posalt in drivers[pid][gene]:
                text_face = ete3.TextFace(gene + append,
                                          fsize=vertical_margins[pid] - 5)
                incongruous_samples = set()
                alt_samples = set()
                dels = set()
                if pid in deletions and gene in deletions[pid] and posalt[
                        0] in deletions[pid][gene]:
                    dels = deletions[pid][gene][posalt[0]]
                branches = getBranchOrBranchesFor(drivers[pid][gene][posalt],
                                                  t, incongruous_samples,
                                                  alt_samples, dels)
                if len(branches) > 1:
                    text_face = ete3.TextFace(gene + append,
                                              fsize=vertical_margins[pid] - 5,
                                              fgcolor="orangered")
                for branch in branches:
                    branch.add_face(text_face, column=1, position="branch-top")
                if checkDelete:
                    for sample in alt_samples:
                        del_look_file.write(str(pid))
                        del_look_file.write("\t" + sample)
                        del_look_file.write("\t" + gene2chrom[gene])
                        del_look_file.write("\t" + posalt[0])
                        del_look_file.write("\t" + gene)
                        del_look_file.write("\t" +
                                            str(sample in incongruous_samples))
                        del_look_file.write("\n")
                if displayDeletions and len(dels) > 0 and len(
                        drivers[pid][gene][posalt]) > 0:
                    text_face = ete3.TextFace(u'Δ ' + gene + append,
                                              fsize=vertical_margins[pid] - 5)
                    #print(str(dels))
                    branches = getBranchOrBranchesFor(dels, t, set(), set(),
                                                      set())
                    for branch in branches:
                        branch.add_face(text_face,
                                        column=1,
                                        position="branch-top")
                if append == "_a":
                    append = "_b"
                elif append == "_b":
                    append = "_c"
                elif append == "_c":
                    append = "_d"
                elif append == "_d":
                    append = "_e"
    else:
        print("No", pid, "in drivers")
Example #6
0
def buildTreeDepthFirst(tranDict, t, nextState):
    if nextState in tranDict:
        possibleTrans = tranDict[nextState]
        for tran in possibleTrans:
            if tran.action == "finishedAction":
                buildTreeDepthFirst(tranDict, t, tran.endState)
            else:
                c = t.add_child(name=tran.action)
                c.dist = tran.percentage
                if float(tran.percentage) > 0.5:
                    c.add_face(ete3.TextFace(tran.action), column=0)
                else:
                    c.add_face(ete3.TextFace("action failed"), column=0)
                buildTreeDepthFirst(tranDict, c, tran.endState)
 def mylayout(node):
     node.set_style(ns)
     if not node.is_leaf():
         ete3.add_face_to_node(ete3.TextFace(node.name),
                               node,
                               column=0,
                               position='branch-top')
         ete3.add_face_to_node(ete3.TextFace('\n'.join(
             getattr(node, 'info', []))),
                               node,
                               column=0,
                               position='branch-bottom')
     if node.support <= 0.5:
         node.set_style(dashed_branch)
     add_img(node)
Example #8
0
def layout(node):
    """ layout for ete2 tree plotting fig """
    if node.is_leaf():
        nameF = ete3.TextFace(node.name, tight_text=False, 
                                         fgcolor="#262626", fsize=8)
        ete3.add_face_to_node(nameF, node, column=0, position="aligned")
        node.img_style["size"] = 0
              
    else:
        if not node.is_root():
            node.img_style["size"] = 0
            node.img_style["shape"] = 'square'
            node.img_style["fgcolor"] = "#262626"   
            if "quartets_total" in node.features:
                ete3.add_face_to_node(ete2.PieChartFace(
                                    percents=[float(node.quartets_sampled_prop), 
                                    100-float(node.quartets_sampled_prop)],
                                    width=15, height=15, colors=PIECOLORS),
                                    node, column=0, position="float-behind")  
            if "bootstrap" in node.features:
                ete3.add_face_to_node(ete2.AttrFace(
                                  "bootstrap", fsize=7, fgcolor='red'),
                                  node, column=0, position="branch-top")  
        else:
            node.img_style["size"] = 0
            if node.is_root():
                node.img_style["size"] = 0
                node.img_style["shape"] = 'square'
                node.img_style["fgcolor"] = "#262626"   
Example #9
0
def _align_leaf_name(tree):
    attr_face = ete3.AttrFace('name')
    text_face = ete3.TextFace('  ')
    for leaf in tree:
        leaf.add_face(text_face, column=0, position='aligned')
        leaf.add_face(attr_face, column=1, position='aligned')
    return 0
def evolve_tree(newick,
                dup_mut=0.00043,
                del_mut=0.0,
                dup_rev=0.00086,
                del_rev=0.0):
    t = copy.deepcopy(newick)

    mutstyle = ete3.NodeStyle()
    mutstyle['fgcolor'] = 'red'

    gen_per_snp = 1 / (
        25 * .0076
    )  #Generations per unit of branch length/SNP; .0076 is SNPs per year, 25 is years per generation
    for node in t.traverse():
        if node.is_root():
            node.add_feature('cnv_state', 'reference')
            continue
        node.add_feature('cnv_state', node.up.cnv_state)
        orig_state = node.cnv_state
        for generation in xrange(
                int(node.dist * gen_per_snp)
        ):  #maybe change this to a power later--would require editing evolve_node function
            evolve_node(node,
                        dup_mut=dup_mut,
                        del_mut=del_mut,
                        dup_rev=dup_rev,
                        del_rev=del_rev)
        if node.cnv_state != orig_state:
            node.set_style(mutstyle)
        if node.is_leaf() and node.cnv_state != 'reference':
            node.add_face(ete3.TextFace(' %s' % (node.cnv_state[:3])), 1,
                          'branch-right')

    return t
Example #11
0
    def my_layout(node):
        name = getattr(node, attribute_name)

        try:
            kmer_full = locale.format("%d", int(node.kmers_full), grouping=True),
        except AttributeError:
            kmer_full = None

        try:
            kmer_reduced = locale.format("%d", int(node.kmers_reduced), grouping=True)
        except AttributeError:
            kmer_reduced = None

        if kmer_full is None:
            if kmer_reduced is None:
                t = name
            else:
                t = "{} [red. {}]".format(name, kmer_reduced)
        else:
            if kmer_reduced is None:
                t = "{} [full {}]".format(name, kmer_full)
            else:
                t = "{} [full {} & red. {}]".format(name, kmer_full, kmer_reduced)

        f = ete3.TextFace(t, tight_text=True)
        ete3.add_face_to_node(f, node, column=0, position="branch-right")
Example #12
0
def draw_tree(plotdir,
              plotname,
              treestr,
              gl_sets,
              all_genes,
              gene_categories,
              ref_label=None,
              arc_start=None,
              arc_span=None):
    etree = ete3.ClusterTree(treestr)
    node_names = set()  # make sure we get out all the genes we put in
    for node in etree.traverse():
        if set_distance_to_zero(node):
            node.dist = 0. if ref_label is not None else 1e-9  # data crashes sometimes with float division by zero if you set it to 0., but simulation sometimes gets screwed up for some other reason (that I don't understand) if it's 1e-9
        # node.dist = 1.
        status = getstatus(gene_categories, node, ref_label=ref_label)
        set_node_style(node, status, len(gl_sets), ref_label=ref_label)
        if node.is_leaf():
            node_names.add(node.name)
    if len(set(all_genes) - node_names) > 0:
        raise Exception('missing genes from final tree: %s' %
                        ' '.join(node_names))

    if ref_label is None:  # have to do it in a separate loop so it doesn't screw up the distance setting
        for node in [n for n in etree.traverse()
                     if n.is_leaf()]:  # yeah I'm sure there's a fcn for that
            node.name = shorten_name(node.name)

    tstyle = ete3.TreeStyle()
    tstyle.show_scale = False
    if not args.leaf_names:
        tstyle.show_leaf_name = False

    # tstyle.mode = 'c'
    # if arc_start is not None:
    #     tstyle.arc_start = arc_start
    # if arc_span is not None:
    #     tstyle.arc_span = arc_span

    write_legend(plotdir)
    if args.title is not None:
        fsize = 13
        tstyle.title.add_face(ete3.TextFace(args.title, fsize=fsize,
                                            bold=True),
                              column=0)
        if args.title_color is not None:
            # tstyle.title.add_face(ete3.CircleFace(fsize, scolors[args.title]), column=1)
            tcol = scolors[
                args.
                title_color] if args.title_color in scolors else args.title_color
            rect_width = 3 if len(args.title) < 12 else 2
            tstyle.title.add_face(ete3.RectFace(width=rect_width * fsize,
                                                height=fsize,
                                                bgcolor=tcol,
                                                fgcolor=None),
                                  column=1)
    suffix = '.svg'
    imagefname = plotdir + '/' + plotname + suffix
    print '      %s' % imagefname
    etree.render(imagefname, tree_style=tstyle)
Example #13
0
    def generate_heatmap(self, ete3_tree, *args):

        for node in ete3_tree:
            if node.is_leaf():
                taxon = node.name

                tc_index = 0
                for args_index in range(0, len(args), 2):
                    taxon_to_classification = args[args_index]
                    class_to_color = args[args_index + 1]

                    try:
                        classification = taxon_to_classification[taxon]
                        color = class_to_color[classification]
                    except:
                        classification = ''
                        color = 'white'

                    class_face = ete3.TextFace(classification,
                                               ftype='Arial',
                                               fsize=2,
                                               fgcolor='white')
                    class_face.background.color = color
                    class_face.hz_align = 1
                    class_face.vt_align = 1
                    class_face.margin_left = 5
                    class_face.margin_right = 5
                    node.add_face(class_face, tc_index, "aligned")
                    tc_index += 1

        return ete3_tree
Example #14
0
 def fam_size_layout(node):
     try:
         node_color = self.graphics_options[node.event]
     except:
         node_color = self.graphics_options['=']
     relative_size = 8 * (node.fam_size / max(family.fam_sizes))
     cf = ete3.CircleFace(
         radius=relative_size * self.graphics_options['scale'],
         color=node_color,
         style='circle'
     )
     cf.opacity = self.graphics_options['opacity']
     node.add_face(cf, column=10, position='float')
     # add the family size number and asterisks to the figure
     famsize_str = '{}{}\n'.format(
         get_pvalue_asterisks(node),
         node.fam_size,
     )
     tf = ete3.TextFace(famsize_str, fsize=4, fgcolor=node_color)
     node.add_face(tf, column=1, position='float')
     # remove the silly default blue node dot
     nstyle = ete3.NodeStyle()
     nstyle['size'] = 0
     node.set_style(nstyle)
     return
 def style_subject_node(color="Black"):
     """Specify the appearance of Subject nodes."""
     face = ete3.TextFace(node.name,
                          fsize=SUBJECT_NODE_FONT_SIZE,
                          fgcolor=color)
     set_face_margin(face)
     node.add_face(face, column=0, position="branch-right")
Example #16
0
def _show_inner_name(tree, tree_format):
    if tree_format != 0:
        return 0
    for node in tree.traverse():
        if (not node.is_leaf()) and (not node.is_root()):
            node_name_face = ete3.TextFace(node.name)
            node.add_face(node_name_face, position='branch-bottom', column=0)
    return 0
Example #17
0
    def summary_tree(self):
        '''
        show a tree that visualizes the total number of expansions and
        contractions across the whole phylogeny for allgene families.
        '''
        def fam_size_piechart_layout(node):
            '''
            the PieChart layout function, defined in local scope so it
            can access class attributes (graphics options).
            '''
            if not node.is_root():
                if self.count_all_expansions:
                    n_exp = node.expansion
                    n_con = node.decrease
                else:
                    n_exp = node.sig_expansions
                    n_con = node.sig_expansions

                if hasattr(self, 'lambda_colors'):
                    circle_color = self.lambda_colors[node.lambda_group]
                else:
                    circle_color = "blue"

                # add a text that shows expansions & contractions, e.g. +10/-20
                exp_cnt_txt = ete3.TextFace(
                    '+{} -{}\n'.format(int(n_exp), int(n_con)), fsize=6,
                    fgcolor=circle_color
                )
                pos = 'aligned' if node.is_leaf() else 'float'
                # add a circle that shows the average expansion
                ete3.faces.add_face_to_node(exp_cnt_txt, node, 1, position=pos)

                # add average expansion info:
                scale_factor = 1 + node.avg_expansion
                avg_exp = '{:+}'.format(round(node.avg_expansion, 2))
                circle = ete3.CircleFace(radius=9 * scale_factor,
                                         color=circle_color,
                                         label={'text': avg_exp, 'color': 'white',
                                                'fontsize': 3 + (2.25*scale_factor)})
                circle.opacity = self.graphics_options['opacity']
                ete3.faces.add_face_to_node(circle, node, 2, position='float')
            nstyle = ete3.NodeStyle()
            nstyle['size'] = 0
            node.set_style(nstyle)
            return
        t = self.tree
        ts = ete3.TreeStyle()
        header = 'Family expansions and contractions'
        if hasattr(self, "lambdas"):
            header += ('\nmin lambda: {} '
                       '(blue)\nmax lambda: {} (red)').format(
                        min(self.lambdas.values()), max(self.lambdas.values()))
        ts.title.add_face(ete3.TextFace(header, fsize=8), column=0)
        ts.scale = self.graphics_options['pixels_per_mya']  # pixels per million years
        ts.layout_fn = fam_size_piechart_layout
        self.show_or_dump_tree(tree_obj=t, tree_style=ts, fname='summary')
        return
Example #18
0
def branch_category_layout(node):
    nstyle = ete3.NodeStyle()
    nstyle['size'] = 0
    nstyle["hz_line_width"] = nstyle["vt_line_width"] = 1
    nstyle["hz_line_color"] = node.color
    nstyle["vt_line_color"] = node.color
    nlabel = node.name+'|'+str(node.numerical_label)
    nlabelFace = ete3.TextFace(nlabel, fsize=4, fgcolor=node.color)
    ete3.add_face_to_node(face=nlabelFace, node=node, column=1, aligned=False, position="branch-right")
    node.set_style(nstyle)
Example #19
0
    def _node_layout(node):
        global _circle_tested
        node.img_style["size"] = 0

        if node.is_leaf():
            if node.name not in annotation:
                print('Got unknown leaf "%s"' % (node.name))
                return
            size = annotation[node.name]
        else:
            children = COMMA.join(sorted([leaf.name for leaf in node]))
            if children not in annotation:
                print('Got unknown node ' + children)
                return
            size = annotation[children]
        dimension = 4 * math.sqrt(size)
        labeld = {
            'text': "%d" % (size),
            'color': 'white',
            'font': 'Helvetica',
            'size': 8
        }
        if size % 2:
            clabel = dict(labeld)
            if size == 35:
                if not _circle_tested:
                    _circle_tested = True
                    clabel['text'] = 'Ly'
                clabel['size'] = 12
                thisFace = ete.CircleFace(dimension / 2,
                                          "steelblue",
                                          "circle",
                                          label=clabel)
            elif size == 43:
                clabel['size'] = 6
                del clabel['color']
                thisFace = ete.CircleFace(dimension / 2,
                                          "steelblue",
                                          "sphere",
                                          label=clabel)
            else:
                thisFace = ete.CircleFace(dimension / 2,
                                          "steelblue",
                                          "sphere",
                                          label="%d" % (size))
        else:
            thisFace = ete.RectFace(dimension,
                                    dimension,
                                    'green',
                                    'blue',
                                    label=labeld)
        thisFace.opacity = 0.7
        ete.add_face_to_node(thisFace, node, column=0, position="float")
        textF = ete.TextFace(str(size), fsize=12, fgcolor="steelblue")
        ete.add_face_to_node(textF, node, column=0, position="aligned")
Example #20
0
def build_tree(jsonNode, tr=ete3.Tree(), depth=0):
    if type(jsonNode) is dict:
        for key, value in jsonNode.items():
            this_node = tr.add_child(name=str(jsonNode))
            face = ete3.TextFace(key)
            this_node.add_face(face, column=1, position="branch-top")
            for child in value:
                build_tree(child, this_node, depth + 1)
    else:
        tr.add_child(name=str(jsonNode))
    return tr
Example #21
0
    def add_child(self,
                  attr,
                  val,
                  cond=None,
                  df=None,
                  target=None,
                  continuous=False):
        if attr is None:
            if not continuous:
                distribution = round(len(df[df[target] == val]) / len(df),
                                     2) * 100
                msg = ' {}: {}% '.format(val, int(distribution))
            else:
                msg = ' {}: {} '.format(target, round(val, 2))
        elif not continuous:
            msg = ' {} = {} '.format(attr, val)
        else:
            msg = ' {} {} {} '.format(attr, '{}', round(val, 2))

        if len(self.children) == 0:
            ete3_child = self.ete3_node.add_child(name=msg)
        else:
            ete3_child = self.children[-1].ete3_node.add_sister()

        if attr is not None and not continuous:
            ete3_child.add_face(ete3.TextFace(msg),
                                column=0,
                                position='branch-right')
        elif attr is not None and len(self.children) == 0:
            ete3_child.add_face(ete3.TextFace(msg.format('<=')),
                                column=0,
                                position='branch-right')
        elif attr is not None:
            ete3_child.add_face(ete3.TextFace(msg.format('>')),
                                column=0,
                                position='branch-right')

        node = Tree(attr, val, cond, ete3_node=ete3_child)
        self.children.append(node)

        return node
Example #22
0
def draw_tree(plotdir, plotname, treestr, gl_sets, all_genes, gene_categories, ref_label=None, arc_start=None, arc_span=None):
    etree = ete3.ClusterTree(treestr)
    node_names = set()  # make sure we get out all the genes we put in
    for node in etree.traverse():
        if set_distance_to_zero(node):
            node.dist = 0. if ref_label is not None else 1e-9  # data crashes sometimes with float division by zero if you set it to 0., but simulation sometimes gets screwed up for some other reason (that I don't understand) if it's 1e-9
        # node.dist = 1.
        status = getstatus(gene_categories, node, ref_label=ref_label)
        set_node_style(node, status, len(gl_sets), ref_label=ref_label)
        if node.is_leaf():
            node_names.add(node.name)
    if len(set(all_genes) - node_names) > 0:
        raise Exception('missing genes from final tree: %s' % ' '.join(node_names))

    if args.param_dirs is not None:
        countfo = OrderedDict()
        for label, pdir in zip(args.glslabels, args.param_dirs):  # it would be cleaner to do this somewhere else
            if pdir == 'None':  # not the best way to do this
                continue
            countfo[label] = utils.read_overall_gene_probs(pdir, normalize=True)[args.region]
        for node in etree.traverse():
            node.countstr = '%s' % ' '.join([('%.2f' % (100 * cfo[node.name])) if node.name in cfo else '-' for cfo in countfo.values()])

    if ref_label is None:  # have to do it in a separate loop so it doesn't screw up the distance setting
        for node in [n for n in etree.traverse() if n.is_leaf()]:  # yeah I'm sure there's a fcn for that
            node.name = utils.shorten_gene_name(node.name)

    tstyle = ete3.TreeStyle()
    tstyle.show_scale = False

    if len(args.glslabels) > 1:
        write_legend(plotdir)
    if args.title is not None:
        fsize = 13
        tstyle.title.add_face(ete3.TextFace(args.title, fsize=fsize, bold=True), column=0)
        if args.title_color is not None:
            # tstyle.title.add_face(ete3.CircleFace(fsize, scolors[args.title]), column=1)
            tcol = scolors[args.title_color] if args.title_color in scolors else args.title_color
            rect_width = 3 if len(args.title) < 12 else 2
            tstyle.title.add_face(ete3.RectFace(width=rect_width*fsize, height=fsize, bgcolor=tcol, fgcolor=None), column=1)
    suffix = '.svg'
    imagefname = plotdir + '/' + plotname + suffix
    print '      %s' % imagefname
    etree.render(utils.insert_before_suffix('-leaf-names', imagefname), tree_style=tstyle)
    tstyle.show_leaf_name = False
    etree.render(imagefname, tree_style=tstyle)

    # NOTE all the node names are screwed up after this, so you'll have to fix them if you add another step
    if args.param_dirs is not None:
        for node in etree.traverse():
            node.name = node.countstr
        tstyle.show_leaf_name = True
        etree.render(utils.insert_before_suffix('-gene-counts', imagefname), tree_style=tstyle)
Example #23
0
def branch_state_layout(node):
    nstyle = ete3.NodeStyle()
    nstyle['size'] = 0
    nstyle["hz_line_width"] = nstyle["vt_line_width"] = 1
    nstyle["hz_line_color"] = node.color
    nstyle["vt_line_color"] = node.color
    if node.is_leaf():
        nlabel = str(node.state)+'|'+node.name
    else:
        nlabel = str(node.state)
    nlabelFace = ete3.TextFace(nlabel, fsize=6, fgcolor=node.color)
    ete3.add_face_to_node(face=nlabelFace, node=node, column=1, aligned=False, position="branch-right")
    node.set_style(nstyle)
Example #24
0
def _main():
    print("Reading matrix from file")
    root = None
    output_file_name = None
    if len(sys.argv) < 2:
        print("Please supply at least one path to a distance matrix")
        quit(1)
    elif sys.argv[1] == "matrix":  # Usage: python3 PhyloTree.py matrix OUTPUT_FILE_NAME [PATH_TO_INPUT_MATRICES . . .]
        node_names_list = []
        dist_mat_list = []
        for mat in sys.argv[3:]:
            (node_names, dist_mat) = Utility.read_distance_matrix(mat)
            node_names_list.append(node_names)
            dist_mat_list.append(dist_mat)
        (node_names, dist_mat) = Utility.combine_distance_matrices(node_names_list, dist_mat_list, norm=False)
        print("Writing combined matrix to {}".format(sys.argv[2]))
        Utility.write_distance_matrix(dist_mat, node_names, sys.argv[2])
        exit(0)
    elif len(sys.argv) == 2:  # Only one matrix provided
        # Read the distance matrix in to memory
        (node_names, dist_mat) = Utility.read_distance_matrix(sys.argv[1])
        print("Creating tree with Neighbor-Joining from 1 matrix")
        # Our neighbor joining algorithm creates rooted trees
        # the root is created a node in the middle of the final edge added to the tree
        root = create_nj_tree(node_names, dist_mat)
        gene_name = sys.argv[1].split("/")[-1]
        output_file_name = "{}_tree.png".format(gene_name)
    else:  # more than one matrix, average them then make tree
        node_names_list = []
        dist_mat_list = []
        for mat in sys.argv[1:]:
            (node_names, dist_mat) = Utility.read_distance_matrix(mat)
            node_names_list.append(node_names)
            dist_mat_list.append(dist_mat)
        (node_names, dist_mat) = Utility.combine_distance_matrices(node_names_list, dist_mat_list, norm=False)
        print("Creating tree with Neighbor-Joining from {} matrices".format(len(sys.argv[1:])))
        root = create_nj_tree(node_names, dist_mat)
        system_name = sys.argv[1].split("/")[-1]
        output_file_name = "{}_combined_tree.png".format(system_name)

    ts = ete3.TreeStyle()
    ts.show_branch_length = True
    ts.title.add_face(ete3.TextFace(output_file_name[0:-4], fsize=20), column=0)
    nstyle = ete3.NodeStyle()
    nstyle["shape"] = "sphere"
    nstyle["size"] = 6
    nstyle["fgcolor"] = "darkred"
    for n in root.traverse():
        n.set_style(nstyle)
    root.show(tree_style=ts)
    root.render(output_file_name, tree_style=ts, units="px", w=600)
Example #25
0
def _build_node_text_face(node, color_node_labels, color, label_colors,
                          fontsize, rename_function):
    if color_node_labels:
        if label_colors is None:
            node_color = color
        elif type(label_colors) == dict:
            node_color = label_colors.get(node.name, '#000000')
        elif type(label_colors) in [list, tuple]:
            node_color = color if node.name in label_colors else '#000000'
        else:
            node_color = '#000000'
    else:
        node_color = '#000000'
    node_name = node.name if rename_function is None else rename_function(
        node.name)
    tf = ete3.TextFace(node_name, fsize=fontsize, fgcolor=node_color)
    return tf
Example #26
0
    def process_tree(self, t):

        # Add faces
        if self.attribute:
            values = set([getattr(l, self.attribute) for l in t.get_leaves()])
            colours = get_colour_set(len(values))
            colour_map = dict(zip(values, colours))
            for l in t.iter_leaves():
                mycolour = colour_map[getattr(l, self.attribute)]
                if not self.dummy:
                    l.add_face(
                        ete3.CircleFace(radius=10,
                                        color=mycolour,
                                        style="sphere"), 0)

        # Apply labels
        if not self.dummy:
            for l in t.iter_leaves():
                l.add_face(ete3.TextFace(getattr(l, self.label)), 1)

        # Plot or save
        if self.output:
            kw = {}
            if self.height or self.width:
                kw["h"] = self.height
                kw["w"] = self.width
                kw["units"] = self.units
                kw["dpi"] = self.dpi
            if self.multiple:
                base, ext = os.path.splitext(self.output)
                filename = base + ("_%06d" % (self.n + 1)) + ext
            else:
                filename = self.output
            if not self.dummy:
                t.render(filename, ultrametric, tree_style=self.ts, **kw)
        else:  # pragma: no cover
            if not self.dummy:
                t.show(ultrametric, tree_style=self.ts)

        self.n += 1

        if self.multiple:
            return None
        else:
            raise StopIteration
def make_tree_vis(newick_tree, gene_name, file_name):
    """
    Create visualizations of maximum-likelihood trees generated using ete
    """
    tree = et.Tree(newick_tree)

    #Customizations
    nifty = et.TreeStyle()
    nifty.show_branch_support = True
    nifty.branch_vertical_margin = 15
    nifty.scale = 10

    #Leaf node customizations
    style_L = et.NodeStyle()
    style_L['shape'] = 'sphere'
    style_L['size'] = 10
    style_L['fgcolor'] = '#6BC245'
    style_L['hz_line_type'] = 1
    style_L['hz_line_color'] = '#cccccc'

    #Internal node customizations
    style_I = et.NodeStyle()
    style_I['shape'] = 'sphere'
    style_I['size'] = 10
    style_I['fgcolor'] = '#5271FF'
    style_I['hz_line_type'] = 1
    style_I['hz_line_color'] = '#cccccc'

    #Apply stylizations
    for node in tree.traverse():
        if node.is_leaf():
            node.set_style(style_L)
        else:
            node.set_style(style_I)

    #Annotate root of tree with gene name
    gene_label = et.TextFace(gene_name, ftype="Helvetica", fsize=5,\
                             fgcolor='green')

    tree.add_face(gene_label, column=0, position="branch-top")

    tree.render(file_name + "_tree.png", tree_style=nifty)
Example #28
0
        def fam_size_piechart_layout(node):
            '''
            the PieChart layout function, defined in local scope so it
            can access class attributes (graphics options).
            '''
            if not node.is_root():
                if self.count_all_expansions:
                    n_exp = node.expansion
                    n_con = node.decrease
                else:
                    n_exp = node.sig_expansions
                    n_con = node.sig_expansions

                if hasattr(self, 'lambda_colors'):
                    circle_color = self.lambda_colors[node.lambda_group]
                else:
                    circle_color = "blue"

                # add a text that shows expansions & contractions, e.g. +10/-20
                exp_cnt_txt = ete3.TextFace(
                    '+{} -{}\n'.format(int(n_exp), int(n_con)), fsize=6,
                    fgcolor=circle_color
                )
                pos = 'aligned' if node.is_leaf() else 'float'
                # add a circle that shows the average expansion
                ete3.faces.add_face_to_node(exp_cnt_txt, node, 1, position=pos)

                # add average expansion info:
                scale_factor = 1 + node.avg_expansion
                avg_exp = '{:+}'.format(round(node.avg_expansion, 2))
                circle = ete3.CircleFace(radius=9 * scale_factor,
                                         color=circle_color,
                                         label={'text': avg_exp, 'color': 'white',
                                                'fontsize': 3 + (2.25*scale_factor)})
                circle.opacity = self.graphics_options['opacity']
                ete3.faces.add_face_to_node(circle, node, 2, position='float')
            nstyle = ete3.NodeStyle()
            nstyle['size'] = 0
            node.set_style(nstyle)
            return
    def plot_tree(self, pdf_fn):
        print("Plotting", pdf_fn, file=sys.stderr)
        ts = ete3.TreeStyle()
        ant = self.antibiotics.upper()
        ts.title.add_face(ete3.TextFace("{}{}".format(18 * " ", ant),
                                        fsize=60),
                          column=0)
        ts.show_leaf_name = False

        for n in self.tree.traverse():
            taxid = n.name
            nstyle = ete3.NodeStyle()
            res_cat = self.res_dict[taxid]
            nstyle["fgcolor"] = cat_to_color[res_cat]
            nstyle["size"] = 15
            n.set_style(nstyle)

            if n.is_leaf():
                N = ete3.AttrFace("name", fsize=20)
                n.add_face(N, 0)
                #add_face_to_node(N, n, 0, position="aligned")
                #n.add_face_to_node(ete3.TextFace("XXX"))

        self.tree.render(pdf_fn, tree_style=ts)
Example #30
0
def set_meta_styles(args, etree, tstyle):
    lbfo = args.metafo[args.lb_metric]
    if args.lb_metric == 'lbr':  # remove zeroes
        lbfo = {u : (math.log(v) if args.log_lbr else v) for u, v in lbfo.items() if v > 0}
    lbvals = lbfo.values()
    if len(lbvals) == 0:
        return
    lb_smap = plotting.get_normalized_scalar_map(lbvals, 'viridis', hard_min=get_scale_min(args.lb_metric, lbvals) if args.lb_metric=='cons-dist-aa' else None)
    lb_min, lb_max = min(lbvals), max(lbvals)

    affyfo = None
    if args.affy_key in args.metafo and set(args.metafo[args.affy_key].values()) != set([None]):
        affyfo = args.metafo[args.affy_key]
        if args.lb_metric in affy_metrics:
            affyvals = affyfo.values()
            affy_smap = plotting.get_normalized_scalar_map([a for a in affyvals if a is not None], 'viridis')
        elif args.lb_metric in delta_affy_metrics:
            delta_affyvals = set_delta_affinities(etree, affyfo)
            delta_affy_increase_smap = plotting.get_normalized_scalar_map([v for v in delta_affyvals if v > 0], 'Reds', remove_top_end=True) if len(delta_affyvals) > 0 else None
            delta_affy_decrease_smap = plotting.get_normalized_scalar_map([abs(v) for v in delta_affyvals if v < 0], 'Blues', remove_top_end=True) if len(delta_affyvals) > 0 else None
        else:
            assert False

    for node in etree.traverse():
        node.img_style['size'] = 0
        rfsize = 0
        bgcolor = plotting.getgrey()
        if args.lb_metric in affy_metrics:
            if node.name not in lbfo:  # really shouldn't happen
                print '  %s missing lb info for node \'%s\'' % (utils.color('red', 'warning'), node.name)
                continue
            if affyfo is not None:
                rfsize = get_size(lb_min, lb_max, lbfo[node.name])
                if node.name in affyfo:
                    bgcolor = plotting.get_smap_color(affy_smap, affyfo, key=node.name)
            else:
                rfsize = 5
                bgcolor = plotting.get_smap_color(lb_smap, lbfo, key=node.name)
        elif args.lb_metric in delta_affy_metrics:
            node.img_style['vt_line_color'] = plotting.getgrey()  # if they're black, it's too hard to see the large changes in affinity, since they're very dark (at least with current color schemes)
            # rfsize = get_size(lb_min, lb_max, lbfo[node.name]) if node.name in lbfo else 1.5
            rfsize = 5 if node.name in lbfo else 1.5
            bgcolor = plotting.get_smap_color(lb_smap, lbfo, key=node.name)
            if affyfo is not None and delta_affy_increase_smap is not None and node.affinity_change is not None:
                # tface = ete3.TextFace(('%+.4f' % node.affinity_change) if node.affinity_change != 0 else '0.', fsize=3)
                # node.add_face(tface, column=0)
                if node.affinity_change > 0:  # increase
                    node.img_style['hz_line_color'] = plotting.get_smap_color(delta_affy_increase_smap, None, val=node.affinity_change)
                    node.img_style['hz_line_width'] = 1.2
                elif node.affinity_change < 0:  # decrease
                    node.img_style['hz_line_color'] = plotting.get_smap_color(delta_affy_decrease_smap, None, val=abs(node.affinity_change))
                    node.img_style['hz_line_width'] = 1.2
                else:
                    node.img_style['hz_line_color'] = plotting.getgrey()
        if args.queries_to_include is not None and node.name in args.queries_to_include:
            tface = ete3.TextFace(node.name, fsize=3, fgcolor='red')
            node.add_face(tface, column=0)
        rface = ete3.RectFace(width=rfsize, height=rfsize, bgcolor=bgcolor, fgcolor=None)
        rface.opacity = opacity
        node.add_face(rface, column=0)

    affy_label = args.affy_key.replace('_', ' ')
    if args.lb_metric in affy_metrics:
        if affyfo is None:
            add_legend(tstyle, args.lb_metric, lbvals, lb_smap, lbfo, 0, n_entries=4)
        else:
            add_legend(tstyle, args.lb_metric, lbvals, None, lbfo, 0, n_entries=4)
            add_legend(tstyle, affy_label, [a for a in affyvals if a is not None], affy_smap, affyfo, 3)
    elif args.lb_metric in delta_affy_metrics:
        add_legend(tstyle, args.lb_metric, lbvals, lb_smap, lbfo, 0, reverse_log=args.log_lbr)
        if affyfo is not None:
            add_legend(tstyle, '%s decrease' % affy_label, [abs(v) for v in delta_affyvals if v < 0], delta_affy_decrease_smap, affyfo, 3, add_sign='-', no_opacity=True)
            add_legend(tstyle, '%s increase' % affy_label, [v for v in delta_affyvals if v > 0], delta_affy_increase_smap, affyfo, 6, add_sign='+', no_opacity=True)