Ejemplo n.º 1
0
def draw_ete2_tree(organism, snplist, tree_file_name, config, c):
    '''Draws a phylogenetic tree using ETE2

    Keyword arguments:
    organism -- the organism of which to make a tree
    snplist -- a list of the SNP names, positions and state
    file_name -- the name of the out-file _tree.pdf will be added

    '''
    newick = tree_to_newick(organism, config, c)
    tree = ete2.Tree(newick, format=1)
    tree_depth = int(tree.get_distance(tree.get_farthest_leaf()[0]))
    for n in tree.traverse():
        # Nodes are set to red colour
        nstyle = ete2.NodeStyle()
        nstyle["fgcolor"] = "#BE0508"
        nstyle["size"] = 10
        nstyle["vt_line_color"] = "#000000"
        nstyle["hz_line_color"] = "#000000"
        nstyle["vt_line_type"] = 0
        nstyle["hz_line_type"] = 0
        nstyle["vt_line_width"] = 2
        nstyle["hz_line_width"] = 2
        for snp in snplist:
            if n.name == snp[0]:
                if snp[1] == snp[3]:
                    # If the SNP is Derived in snplist,
                    # change appearance of node
                    nstyle["fgcolor"] = "#99FF66"
                    nstyle["size"] = 15
                    nstyle["vt_line_color"] = "#000000"
                    nstyle["hz_line_color"] = "#000000"
                    nstyle["vt_line_type"] = 0
                    nstyle["hz_line_type"] = 0
                elif snp[3] == "-":
                    # If the SNP is missing due to a gap, make it grey
                    nstyle["fgcolor"] = "#DDDDDD"
                    nstyle["size"] = 10
                    nstyle["vt_line_color"] = "#DDDDDD"
                    nstyle["hz_line_color"] = "#DDDDDD"
                    nstyle["vt_line_type"] = 1
                    nstyle["hz_line_type"] = 1
        n.set_style(nstyle)
    ts = ete2.TreeStyle()
    ts.show_leaf_name = False  # Do not print(leaf names, they are added in layout)
    ts.show_scale = False  # Do not show the scale
    ts.layout_fn = CanSNPer_tree_layout  # Use the custom layout
    ts.optimal_scale_level = 'full'  # Fully expand the branches of the tree
    if config["dev"]:
        print("#[DEV] Tree file: %s" % tree_file_name)
    tree.render(tree_file_name, tree_style=ts, w=tree_depth * 500)
Ejemplo n.º 2
0
def draw_tree(ptree, labels=None):
    root = ete2.Tree(name='root')
    T = [
        ete2.Tree(name=(str(node) + '[' + str(i) + ']'))
        for i, node in enumerate(ptree.nodes)
    ]
    if labels is not None:
        for t, lab in zip(T, labels):
            t.name += '{' + str(lab) + '}'
    for i, p in enumerate(ptree.parents):
        if p > 0:
            T[p].add_child(T[i])
        else:
            root.add_child(T[i])
    cmap = color_map(max(labels) + 2)
    for t, l in zip(T, labels):
        ns = ete2.NodeStyle()
        ns['bgcolor'] = cmap[l]
        t.set_style(ns)
        if not t.is_leaf():
            t.add_face(ete2.TextFace(t.name), column=0, position='branch-top')
    root.show()
Ejemplo n.º 3
0
 def _make_tree_figure(self,
                       tree,
                       fig,
                       colors,
                       orders,
                       root_name,
                       scale=None,
                       branch_vert_margin=None,
                       fontsize=12,
                       show_names=True,
                       name_field='seq_id',
                       rename_function=None,
                       color_node_labels=False,
                       label_colors=None,
                       tree_orientation=0,
                       min_order_fraction=0.1,
                       show_root_name=False,
                       chain=None,
                       linked_alignment=None,
                       alignment_fontsize=11,
                       alignment_height=50,
                       alignment_width=50,
                       compact_alignment=False,
                       scale_factor=1,
                       linewidth=1,
                       show_scale=False):
     if show_names is True:
         if chain == 'heavy':
             show_names = [
                 p.heavy[name_field] for p in self.pairs
                 if p.heavy is not None
             ]
         else:
             show_names = [
                 p.light[name_field] for p in self.pairs
                 if p.light is not None
             ]
     elif show_names is False:
         show_names = []
     if show_root_name is True:
         show_names.append(root_name)
     if linked_alignment is not None:
         t = ete2.PhyloTree(tree,
                            alignment=linked_alignment,
                            alg_format='fasta')
         ete2.faces.SequenceItem = MySequenceItem
     else:
         t = ete2.Tree(tree)
     t.set_outgroup(t & root_name)
     # style the nodes
     for node in t.traverse():
         if orders is not None:
             leaves = node.get_leaf_names()
             order_count = Counter([orders[l] for l in leaves])
             for order in sorted(order_count.keys()):
                 if float(order_count[order]) / len(
                         leaves) >= min_order_fraction:
                     color = colors[order]
                     break
         else:
             color = colors.get(node.name, '#000000')
         if linked_alignment is not None:
             node.add_feature('aln_fontsize', alignment_fontsize)
             node.add_feature('aln_height', alignment_height)
             node.add_feature('aln_width', alignment_width)
             node.add_feature('fontsize', fontsize)
             node.add_feature('format', 'seq')
             node.add_feature('scale_factor', scale_factor)
         style = ete2.NodeStyle()
         style['size'] = 0
         style['vt_line_width'] = float(linewidth)
         style['hz_line_width'] = float(linewidth)
         style['vt_line_color'] = color
         style['hz_line_color'] = color
         style['vt_line_type'] = 0
         style['hz_line_type'] = 0
         # else:
         #     style['size'] = 0
         #     style['vt_line_width'] = float(linewidth)
         #     style['hz_line_width'] = float(linewidth)
         #     style['vt_line_color'] = color
         #     style['hz_line_color'] = color
         #     style['vt_line_type'] = 0
         #     style['hz_line_type'] = 0
         if node.name in show_names:
             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 = ete2.TextFace(node_name,
                                fsize=fontsize,
                                fgcolor=node_color)
             # tf.fsize = fontsize
             node.add_face(tf, column=0)
             # style['fgcolor'] = hex_to_rgb(node_color)
         # else:
         #     if hasattr(node, "sequence"):
         #         node.add_face(ete2.SeqMotifFace(seq=node.sequence,
         #                                         seqtype="aa",
         #                                         height=50,
         #                                         seq_format="seq"), column=0, position="aligned")
         node.set_style(style)
     t.dist = 0
     ts = ete2.TreeStyle()
     if linked_alignment is not None:
         ts.layout_fn = self._phyloalignment_layout_function
     ts.orientation = tree_orientation
     ts.show_leaf_name = False
     if scale is not None:
         ts.scale = int(scale)
     if branch_vert_margin is not None:
         ts.branch_vertical_margin = float(branch_vert_margin)
     ts.show_scale = show_scale
     # ladderize
     t.ladderize()
     # render the tree
     t.render(fig, tree_style=ts)
Ejemplo n.º 4
0
def tree_draw(tree_file,
              tree_name=None,
              order_vector_file=None,
              cell_colors_file=None,
              clustering_colors_file=None,
              clustering_sizes_file=None,
              intermediate_node_sizes_file=None,
              intermediate_node_labels_file=None,
              leaf_labels_file=None,
              legend_file=None,
              duplicate_file=None,
              tree_scale='linear',
              tree_rotation=True,
              font_size=7,
              font_legend=7,
              node_size=3,
              scale_rate=None,
              distance_factor=1,
              y_scale=False):

    t = ete2.Tree(newick=tree_file, format=1)
    ts = ete2.TreeStyle()
    if tree_rotation:
        ts.rotation = 90
    ts.show_leaf_name = True
    ts.show_scale = False
    ts.scale = 1
    if tree_name:
        ts.title.add_face(ete2.TextFace(tree_name, fsize=20), column=0)

    styles = {}
    max_dist = 0

    # initialize all nodes and branches
    for n in t.traverse():
        styles[n.name] = dict()
        styles[n.name]['style'] = ete2.NodeStyle()
        styles[n.name]['style']['fgcolor'] = 'black'
        max_dist = max(max_dist, n.dist)

    # calculate the scale for the tree (log, linear and right size)
    if tree_scale == 'log':
        max_dist = 0
    root = t.get_tree_root()
    last_leaf = root.get_farthest_leaf()
    ts.y_axis['scale_min_value'] = root.dist
    ts.y_axis['scale_max_value'] = last_leaf.dist

    for n in t.traverse():
        if tree_scale == 'log':
            if n == root:
                styles[n.name]['dist'] = 0
            else:
                father_path = 0
                for ancestor in n.get_ancestors():
                    father_path += styles[ancestor.name]['dist']

                dist = math.log10(n.get_distance(root) * distance_factor +
                                  1) - father_path
                if dist < 0:
                    dist = 0
                styles[n.name]['dist'] = dist
                max_dist = max(max_dist, dist)

        elif tree_scale == 'linear':
            if max_dist > 1:
                styles[n.name]['dist'] = round(n.dist / max_dist)
            else:
                styles[n.name]['dist'] = n.dist

    # leaf styles and update distance
    if not scale_rate:
        scale_rate = max(1000, round(1 / max_dist))
    for n in t.traverse():
        if 'dist' in styles[n.name]:
            n.dist = styles[n.name]['dist'] * scale_rate
        if not n.is_leaf():
            styles[n.name]['style']["size"] = 0
        else:
            styles[n.name]['style']["size"] = node_size

    # add bootstrap values to the branches (size of the node)
    if intermediate_node_sizes_file:
        bootsrtap_sizes = utils.get_bootsrtap_size(
            intermediate_node_sizes_file)
        for branch, size in bootsrtap_sizes.iteritems():
            styles[branch]['style']["size"] = size
            styles[branch]['style']['fgcolor'] = 'black'

    # add colors to the leafs
    if cell_colors_file:
        cells_colors = utils.get_cells_colors(cell_colors_file)
        for name, color in cells_colors.iteritems():
            styles[name]['style']['fgcolor'] = color

    # reorder the tree by pre-proses if possible
    if order_vector_file:
        leaf_order = utils.get_leaf_order(order_vector_file)
        for n in t.traverse('postorder'):
            if n.get_descendants():
                a = ''
                for leaf in n.get_descendants(strategy='postorder'):
                    if leaf.is_leaf():
                        if not a:
                            a = leaf
                b = n.get_descendants(strategy='preorder')[-1]

                if a.is_leaf() and b.is_leaf():
                    if leaf_order[a.name] > leaf_order[b.name]:
                        left, right = n.children
                        n.children = [right, left]

    # add width to branches
    if clustering_sizes_file:
        t, styles = size_clustering(t, styles, clustering_sizes_file)

    # add colors to branches
    if clustering_colors_file:
        t, ts, styles = color_clustering(t, ts, styles, clustering_colors_file)

    # add new leaf labels
    if leaf_labels_file:
        cells_labels = utils.get_cells_labels(leaf_labels_file)
        ts.show_leaf_name = False
        for name, label in cells_labels.iteritems():
            nodes = t.search_nodes(name=name)
            assert len(nodes) == 1
            node = nodes[0]
            if name in cells_colors:
                name_face = ete2.faces.TextFace(cells_labels[name],
                                                fsize=font_size,
                                                fgcolor=cells_colors[name])
            else:
                name_face = ete2.faces.TextFace(cells_labels[name],
                                                fsize=font_size)

            name_face.margin_left = 3
            node.add_face(name_face, column=0)

    # add duplicate tags to nodes
    if duplicate_file:
        dup_labels = utils.get_dup_labels(duplicate_file)
        for name, color in dup_labels.iteritems():
            node = node_check(name, t)
            if not node:
                continue
            dup_face = ete2.faces.TextFace('*', fsize=10, fgcolor=color)
            dup_face.margin_left = 5
            node.add_face(dup_face, column=1)

    # add legend to the tree
    if legend_file:
        legend = utils.get_legend(legend_file)
        for mark in legend.keys():
            ts.legend.add_face(ete2.faces.CircleFace(2, legend[mark]),
                               column=0)
            legend_txt = ete2.faces.TextFace(mark, fsize=font_legend)
            legend_txt.margin_left = 5
            ts.legend.add_face(legend_txt, column=1)
        ts.legend_position = 4

    # add y-scale to the picture
    if y_scale:

        ts.y_axis['scale_type'] = tree_scale
        ts.y_axis['scale_length'] = last_leaf.dist - root.dist

    # set all the styles
    for n in t.traverse():
        if n.name == 'IDroot':
            n.dist = 0
            n.delete()
        if n.is_root():
            n.dist = 0
            n.delete()
        n.set_style(styles[n.name]['style'])
    root = ete2.faces.CircleFace(2, 'white')
    root.border.width = 1
    root.border.color = 'black'
    t.add_face(root, column=0, position='float')

    # t.render("%%inline", tree_style=ts)
    return t, ts
Ejemplo n.º 5
0
def main():
    non_root_nodes = set()
    tree_dict = dict()
    node_times_dict = collections.defaultdict(list)

    # Read data file
    with open(input_file, 'r') as f:
        for l in f.readlines():
            if l.startswith('('):
                parts = l.split(' ')
                if len(parts) == 2:
                    tuple_txt, time = parts
                    time = float(time)
                    t = eval(tuple_txt)
                    node_times_dict[t].append(time)
                elif len(parts) == 3:
                    parent, child1, child2 = parts
                    parent = eval(parent)
                    child1 = eval(child1)
                    child2 = eval(child2)

                    tree_dict[parent] = (child1, child2)

                    non_root_nodes.add(child1)
                    non_root_nodes.add(child2)
                else:
                    assert False
            else:
                print l

    # Post-process time info
    node_times_dict = {k: sorted(v) for k, v in node_times_dict.iteritems()}

    # Post-process tree info, building an ete2 tree with two nodes for each
    # actual node (to allow us to split the corresponding line segment into
    # two parts in the final drawing).
    root_node_label = [n for n in tree_dict if n not in non_root_nodes][0]
    root_node = ete2.TreeNode()
    root_node_2 = ete2.TreeNode()
    root_node.add_child(root_node_2)
    tree_nodes_dict = {root_node_label: root_node}
    tree_nodes_2_dict = {root_node_label: root_node_2}

    def build_tree(node_label):
        if node_label in tree_dict:
            child1_label, child2_label = tree_dict[node_label]

            tree_nodes_dict[child1_label] = ete2.TreeNode()
            tree_nodes_dict[child2_label] = ete2.TreeNode()
            tree_nodes_2_dict[child1_label] = ete2.TreeNode()
            tree_nodes_2_dict[child2_label] = ete2.TreeNode()

            tree_nodes_2_dict[node_label].add_child(
                tree_nodes_dict[child1_label])
            tree_nodes_2_dict[node_label].add_child(
                tree_nodes_dict[child2_label])

            tree_nodes_dict[child1_label].add_child(
                tree_nodes_2_dict[child1_label])
            tree_nodes_dict[child2_label].add_child(
                tree_nodes_2_dict[child2_label])

            build_tree(child1_label)
            build_tree(child2_label)
        else:
            return

    build_tree(root_node_label)

    waiting_style = ete2.NodeStyle()
    waiting_style['hz_line_color'] = '#D62728'
    waiting_style['hz_line_width'] = 1.5
    waiting_style['size'] = 0.0
    waiting_style['fgcolor'] = '#444444'

    active_style = ete2.NodeStyle()
    active_style['hz_line_color'] = '#2CA02C'
    active_style['hz_line_width'] = 1.5
    active_style['size'] = 0
    active_style['fgcolor'] = '#000000'

    for node_label, node in tree_nodes_dict.iteritems():
        t1, t2, t3 = node_times_dict[node_label]
        node.dist = t2 - t1
        node.set_style(waiting_style)

    for node_label, node in tree_nodes_2_dict.iteritems():
        t1, t2, t3 = node_times_dict[node_label]
        node.dist = t3 - t2
        node.set_style(active_style)

    root_node.render(args.output_file)
Ejemplo n.º 6
0
def make_figure(tree,
                timepoints,
                delimiter,
                scale,
                branch_vert_margin,
                fontsize,
                show_name,
                tree_orientation,
                show_scale=False):
    fig = tree.replace('_tree.nw', '_tree.pdf')
    orders = {tp.name: tp.order for tp in timepoints}
    colors = {tp.name: tp.color for tp in timepoints}
    # settins for name showing
    if show_name == 'none':
        show_name = []
    if show_name == 'all':
        show_name = ['mab', 'root', 'input']
    elif show_name == 'no-root':
        show_name = ['input', 'mab']
    elif type(show_name) in [str, unicode]:
        show_name = [
            show_name,
        ]
    # make the tree
    t = ete2.Tree(tree)
    t.set_outgroup(t & "root")
    # style the nodes based on timepoint
    for node in t.traverse():
        earliest = get_earliest_leaf(node.get_leaf_names(), orders, delimiter)
        color = colors[earliest]
        node_type = get_node_type(node.name)
        style = ete2.NodeStyle()
        style['size'] = 0
        style['vt_line_width'] = 1.0
        style['hz_line_width'] = 1.0
        style['vt_line_color'] = color
        style['hz_line_color'] = color
        style['vt_line_type'] = 0
        style['hz_line_type'] = 0
        if node_type in show_name:
            if node_type in ['mab', 'input']:
                name = ' ' + delimiter.join(node.name.split(delimiter)[1:])
            else:
                name = ' ' + node.name
            tf = ete2.TextFace(name)
            tf.fsize = fontsize
            node.add_face(tf, column=0)
            style['fgcolor'] = '#000000'
        node.set_style(style)
    # style the full tree
    # root = (t&"root")
    # nearest_to_root, distance = root.get_closest_leaf()
    # root_node = t.get_common_ancestor(root, nearest_to_root)
    t.dist = 0
    ts = ete2.TreeStyle()
    ts.orientation = tree_orientation
    ts.show_leaf_name = False
    if scale:
        ts.scale = int(scale)
    if branch_vert_margin:
        ts.branch_vertical_margin = float(branch_vert_margin)
    ts.show_scale = False
    # ladderize
    t.ladderize()
    # render the tree
    t.render(fig, tree_style=ts)
Ejemplo n.º 7
0
    "Takifugu_rubripes", "Takifugu_flavidus", "Oreochromis_niloticus",
    "Neolamprologus_brichardi", "Maylandia_zebra", "Pundamilia_nyererei",
    "Oryzias_latipes", "Xiphophorus_maculatus", "Gasterosteus_aculeatus",
    "Gadus_morhua", "Astyanax_mexicanus", "Lepisosteus_oculatus",
    "Petromyzon_marinus"
]
specieslist = xistaltspeciestree
#starttree = "ensembl_amniota23.tree"
starttree = "hg38_100way.tree"
outpdf = "hsXIST_alt1_tree.pdf"

tree = ete.Tree(starttree)
tree.prune(specieslist, preserve_branch_length=False)

for n in tree.traverse():
    style = ete.NodeStyle()
    #style['hz_line_width'] = 1
    #style['vt_line_width'] = 1
    style['size'] = 0
    n.set_style(style)
ts = ete.TreeStyle()
ts.mode = 'r'
ts.show_leaf_name = False
ts.show_scale = False
tree.render(outpdf, tree_style=ts, h=200)
tree.show(tree_style=ts)
"""
grep -f <(grep ">" hg38_multiz100_RPL8_group1_mlocarna.fa | \
cut -c2-) ~/Documents/chang/psoralen/covariation/hg38_100way.name | \
cut -f5 | tr '\n' ',' | sed 's/,/", "/g'