def treelegendtext(whattoprint, color):
    text = TextFace(" %s " % whattoprint)
    text.hz_align = False
    text.fsize = 30
    text.fstyle = 'Bold'
    text.background.color = color
    return text
def prettifyTree(ete_tree,
                 leaf_font_size=32,
                 branch_support_size=20,
                 show_bootstraps=True,
                 title=None,
                 ts=None):
    ''' Perform standardized functions to make the ETE trees easier to read:
    - Make the branch support bigger
    - Make the leaf fonts bigger
    - Turn off elongating branches for visualization purposes (i.e. make sure the real branch lengths are represented)
    - Change both to standard font (Times)
    - Standardize the tree's width (calculate based on the maximum length from the root to a tip)
    - (optional) add title to tree
    '''

    for node in ete_tree.traverse():
        if node.is_leaf():
            # Make text faces with name = the existing node name but with big font.
            # A side effect of this is that we also get the annotations lined up
            F = faces.TextFace(node.name, ftype="Times", fsize=leaf_font_size)
            node.add_face(F, 0, position="aligned")
        else:
            if show_bootstraps:
                # Make branch support bigger
                F = faces.TextFace(node._support,
                                   ftype="Times",
                                   fsize=branch_support_size)
                node.add_face(F, 0, position="branch-top")

    #correct the long root node bug (fixed in next release)
    ete_tree.dist = 0

    # Optionally create a new TreeStyle if we are not passing in an old one.
    if ts is None:
        ts = TreeStyle()

    # This "fixes" the dashed line issue but makes the tree look terrible.
    # There may be no way around this (and it's likely other tree viewers do the same thing
    # but just don't tell you).
    #ts.optimal_scale_level = "full"

    # We made these bigger so lets turn off the default ones too.
    ts.show_branch_support = False
    ts.show_leaf_name = False

    if title is not None:
        ts.title.clear()
        title = TextFace(title)
        title.hz_align = True
        title.fsize = 52
        ts.title.add_face(title, 0)

    return ete_tree, ts
def addCoreDataToTree(ete_tree,
                      runid,
                      sanitized=False,
                      any_org=False,
                      all_org=False,
                      only_org=False,
                      none_org=False,
                      uniq_org=False,
                      color="Black",
                      compare_to_adj_clade=False):
    '''A function to add data related to gene and organism distribution across clusters
    to a core gene tree.

    Optionally (with compare_to_adj_clade) instead of comparing to the organisms in the whole tree,
    we compare to only the organisms in the sister clade at each node.

    See http://etetoolkit.org/docs/latest/reference/reference_treeview.html#color-names for a list
    of valid color names.'''

    cl = getClusterOrgsByRun(runid)

    outgroup = None
    nodenum = 0
    nodeNumToClusterList = {}
    # The strategy really doesn't matter, it's just for aesthetics... and to make sure its always the same.
    for node in ete_tree.traverse("postorder"):
        nodenum += 1
        leafnames = node.get_leaf_names()

        if compare_to_adj_clade:
            outgroup = []
            if node.is_root():
                # The root node has no sisters
                continue
            else:
                # This is a list - there can be more than one sister node
                sisters = node.get_sisters()
                for sister in sisters:
                    outgroup += sister.get_leaf_names()
                    pass
                pass
            pass

        clusters = findGenesByOrganismList(leafnames,
                                           runid,
                                           cl=cl,
                                           sanitized=True,
                                           all_org=all_org,
                                           any_org=any_org,
                                           only_org=only_org,
                                           none_org=none_org,
                                           uniq_org=uniq_org,
                                           outgroup=outgroup)
        numclusters = len(clusters)
        # This is mostly so that I can keep track of progress.
        sys.stderr.write("%d (N%d)\n" % (numclusters, nodenum))
        numFace = TextFace("%d (N%d)" % (numclusters, nodenum),
                           ftype="Times",
                           fsize=24,
                           fgcolor=color)
        node.add_face(numFace, 0, position="branch-bottom")
        nodeNumToClusterList[nodenum] = clusters

    return ete_tree, nodeNumToClusterList