Exemplo n.º 1
0
def get_example_tree():
    # Random tree
    t = Tree()
    t.populate(20, random_branches=True)

    # Some random features in all nodes
    for n in t.traverse():
        n.add_features(weight=random.randint(0, 50))

    # Create an empty TreeStyle
    ts = TreeStyle()

    # Set our custom layout function
    ts.layout_fn = layout

    # Draw a tree 
    ts.mode = "c"

    # We will add node names manually
    ts.show_leaf_name = False
    # Show branch data
    ts.show_branch_length = True
    ts.show_branch_support = True

    return t, ts
Exemplo n.º 2
0
def get_example_tree():
    # Random tree
    t = Tree()
    t.populate(20, random_branches=True)

    # Some random features in all nodes
    for n in t.traverse():
        n.add_features(weight=random.randint(0, 50))

    # Create an empty TreeStyle
    ts = TreeStyle()

    # Set our custom layout function
    ts.layout_fn = layout

    # Draw a tree
    ts.mode = "c"

    # We will add node names manually
    ts.show_leaf_name = False
    # Show branch data
    ts.show_branch_length = True
    ts.show_branch_support = True

    return t, ts
Exemplo n.º 3
0
Arquivo: utils.py Projeto: jhcepas/npr
def render_tree(tree, fname):
    # Generates tree snapshot
    npr_nodestyle = NodeStyle()
    npr_nodestyle["fgcolor"] = "red"
    for n in tree.traverse():
        if hasattr(n, "nodeid"):
            n.set_style(npr_nodestyle)
    ts = TreeStyle()
    ts.show_leaf_name = True
    ts.show_branch_length = True
    ts.show_branch_support = True
    ts.mode = "r"
    iterface = faces.TextFace("iter")
    ts.legend.add_face(iterface, 0)

    tree.dist = 0
    tree.sort_descendants()
    tree.render(fname, tree_style=ts, w=700)
Exemplo n.º 4
0
        T.opacity = 0.8
        # And place as a float face over the tree
        faces.add_face_to_node(T, node, 1, position="aligned")


# Random tree
t = Tree()
t.populate(20, random_branches=True)

# Some random features in all nodes
for n in t.traverse():
    n.add_features(weight=random.randint(0, 50))

# Create an empty TreeStyle
ts = TreeStyle()

# Set our custom layout function
ts.layout_fn = layout

# Draw a tree
ts.mode = "c"

# We will add node names manually
ts.show_leaf_name = False
# Show branch data
ts.show_branch_length = True
ts.show_branch_support = True

t.render("tree_faces.png", w=600, dpi=300, tree_style=ts)
t.show(tree_style=ts)
Exemplo n.º 5
0
        T.opacity = 0.8
        # And place as a float face over the tree
        faces.add_face_to_node(T, node, 1, position="aligned")

# Random tree
t = Tree()
t.populate(20, random_branches=True)

# Some random features in all nodes
for n in t.traverse():
    n.add_features(weight=random.randint(0, 50))

# Create an empty TreeStyle
ts = TreeStyle()

# Set our custom layout function
ts.layout_fn = layout

# Draw a tree 
ts.mode = "c"

# We will add node names manually
ts.show_leaf_name = False
# Show branch data
ts.show_branch_length = True
ts.show_branch_support = True

t.render("tree_faces.png", w=600, dpi=300, tree_style=ts)
t.show(tree_style=ts)
 
Exemplo n.º 6
0
def draw(t, draw_alg=True):
    def ly(node):
        node.img_style['vt_line_width'] = 1
        node.img_style['hz_line_width'] = 1
        if node.is_leaf():
            add_face_to_node(TextFace(
                ' (%s)' % node.name.split()[0].replace("/exon2", ""),
                fsize=10,
                fgcolor='slategrey',
                tight_text=False),
                             node,
                             1,
                             position='branch-right')
            add_face_to_node(TextFace(node.species,
                                      fsize=12,
                                      fgcolor='black',
                                      fstyle='italic',
                                      tight_text=False),
                             node,
                             0,
                             position='branch-right')
            c = 1
            for tname in tracked_clades:
                if tname in node.named_lineage:
                    linF = TextFace(tname, fsize=10, fgcolor='white')
                    linF.margin_left = 3
                    linF.background.color = lin2color[tname]

                    add_face_to_node(linF, node, c, position='aligned')
                    c += 1
            for n in xrange(1, 20 - (c - 1)):
                add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'),
                                 node,
                                 c,
                                 position='aligned')
                c += 1

            if draw_alg and 'sequence' in node.features:
                #seqFace = SequenceFace(node.sequence,"aa",13)
                seqFace = SeqMotifFace(node.sequence, [])
                # [10, 100, "[]", None, 10, "black", "rgradient:blue", "arial|8|white|domain Name"],
                motifs = []
                last_lt = None
                for c, lt in enumerate(node.sequence):
                    if lt != '-':
                        if last_lt is None:
                            last_lt = c

                        if c + 1 == len(node.sequence):
                            start, end = last_lt, c
                            w = end - start
                            motifs.append([
                                start, end, "[]", w, 13, "slategrey",
                                "slategrey", None
                            ])
                            last_lt = None

                    elif lt == '-':
                        if last_lt is not None:
                            start, end = last_lt, c - 1
                            w = end - start
                            motifs.append([
                                start, end, "[]", w, 13, "slategrey",
                                "slategrey", None
                            ])
                            last_lt = None

                if not motifs:
                    print node, node.sequence

                seqFace = SeqMotifFace(node.sequence,
                                       motifs,
                                       intermotif_format="line",
                                       seqtail_format="line",
                                       scale_factor=1)
                add_face_to_node(seqFace, node, 20, aligned=True)

        else:
            if node.up:
                add_face_to_node(TextFace('% 3g' % node.support,
                                          fsize=11,
                                          fgcolor='indianred'),
                                 node,
                                 0,
                                 position='branch-top')

            if hasattr(node, "support2") and node.up:
                add_face_to_node(TextFace('% 3g' % float(node.support2),
                                          fsize=11,
                                          fgcolor='steelblue'),
                                 node,
                                 0,
                                 position='branch-bottom')

        node.img_style['size'] = 0
        node.img_style['hz_line_color'] = 'black'
        node.img_style['vt_line_color'] = 'black'

    colors = random_color(num=len(tracked_clades))
    lin2color = dict([(ln, colors[i]) for i, ln in enumerate(tracked_clades)])
    ts = TreeStyle()
    ts.draw_aligned_faces_as_table = False
    ts.draw_guiding_lines = False
    ts.show_leaf_name = False
    ts.show_branch_support = False
    ts.layout_fn = ly
    print 'Rendering tree.pdf'
    t.render('tree.svg', tree_style=ts)
    t.render('tree.png', tree_style=ts)
Exemplo n.º 7
0
def draw_tree(tree, conf, outfile):
    try:
        from ete_dev import (add_face_to_node, AttrFace, TextFace, TreeStyle, RectFace, CircleFace,
                             SequenceFace, random_color, SeqMotifFace)
    except ImportError as e:
        print e
        return
  
    def ly_basic(node):
        if node.is_leaf():
            node.img_style['size'] = 0
        else:
            node.img_style['size'] = 0
            node.img_style['shape'] = 'square'
            if len(MIXED_RES) > 1 and hasattr(node, "tree_seqtype"):
                if node.tree_seqtype == "nt":
                    node.img_style["bgcolor"] = "#CFE6CA"
                    ntF = TextFace("nt", fsize=6, fgcolor='#444', ftype='Helvetica')
                    add_face_to_node(ntF, node, 10, position="branch-bottom")
            if len(NPR_TREES) > 1 and hasattr(node, "tree_type"):
                node.img_style['size'] = 4
                node.img_style['fgcolor'] = "steelblue"

        node.img_style['hz_line_width'] = 1
        node.img_style['vt_line_width'] = 1
                    
    def ly_leaf_names(node):
        if node.is_leaf():
            spF = TextFace(node.species, fsize=10, fgcolor='#444444', fstyle='italic', ftype='Helvetica')
            add_face_to_node(spF, node, column=0, position='branch-right')
            if hasattr(node, 'genename'):
                geneF = TextFace(" (%s)" %node.genename, fsize=8, fgcolor='#777777', ftype='Helvetica')
                add_face_to_node(geneF, node, column=1, position='branch-right')

    def ly_supports(node):
        if not node.is_leaf() and node.up:
            supFace = TextFace("%0.2g" %(node.support), fsize=7, fgcolor='indianred')
            add_face_to_node(supFace, node, column=0, position='branch-top')
                
    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 xrange(c, len(TRACKED_CLADES)):
                add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'), node, c, position='aligned')
                c+=1

    def ly_full_alg(node):
        pass

    def ly_block_alg(node):
        if node.is_leaf():
            if 'sequence' in node.features:
                seqFace = SeqMotifFace(node.sequence, [])
                # [10, 100, "[]", None, 10, "black", "rgradient:blue", "arial|8|white|domain Name"],
                motifs = []
                last_lt = None
                for c, lt in enumerate(node.sequence):
                    if lt != '-':
                        if last_lt is None:
                            last_lt = c
                        if c+1 == len(node.sequence):
                            start, end = last_lt, c
                            motifs.append([start, end, "()", 0, 12, "slategrey", "slategrey", None])
                            last_lt = None
                    elif lt == '-':
                        if last_lt is not None:
                            start, end = last_lt, c-1
                            motifs.append([start, end, "()", 0, 12, "grey", "slategrey", None])
                            last_lt = None

                seqFace = SeqMotifFace(node.sequence, motifs,
                                       intermotif_format="line",
                                       seqtail_format="line", scale_factor=ALG_SCALE)
                add_face_to_node(seqFace, node, ALG_START_COL, aligned=True)

                
    TRACKED_CLADES = ["Eukaryota", "Viridiplantae",  "Fungi",
                      "Alveolata", "Metazoa", "Stramenopiles", "Rhodophyta",
                      "Amoebozoa", "Crypthophyta", "Bacteria",
                      "Alphaproteobacteria", "Betaproteobacteria", "Cyanobacteria",
                      "Gammaproteobacteria",]
    
    # ["Opisthokonta",  "Apicomplexa"]
    
    colors = random_color(num=len(TRACKED_CLADES), s=0.45)
    lin2color = dict([(ln, colors[i]) for i, ln in enumerate(TRACKED_CLADES)])

    NAME_FACE = AttrFace('name', fsize=10, fgcolor='#444444')
        
    LABEL_START_COL = 10
    ALG_START_COL = 40
    ts = TreeStyle()
    ts.draw_aligned_faces_as_table = False
    ts.draw_guiding_lines = False
    ts.show_leaf_name = False
    ts.show_branch_support = False
    ts.scale = 160

    ts.layout_fn = [ly_basic, ly_leaf_names, ly_supports, ly_tax_labels]

    MIXED_RES = set()
    MAX_SEQ_LEN = 0
    NPR_TREES = []
    for n in tree.traverse():
        if hasattr(n, "tree_seqtype"):
            MIXED_RES.add(n.tree_seqtype)
        if hasattr(n, "tree_type"):
            NPR_TREES.append(n.tree_type)
        seq = getattr(n, "sequence", "")
        MAX_SEQ_LEN = max(len(seq), MAX_SEQ_LEN) 

    if MAX_SEQ_LEN:
        ALG_SCALE = min(1, 1000./MAX_SEQ_LEN)
        ts.layout_fn.append(ly_block_alg)
        
    if len(NPR_TREES) > 1:
        rF = RectFace(4, 4, "steelblue", "steelblue")
        rF.margin_right = 10
        rF.margin_left = 10
        ts.legend.add_face(rF, 0)
        ts.legend.add_face(TextFace(" NPR node"), 1)
        ts.legend_position = 3

    if len(MIXED_RES) > 1:
        rF = RectFace(20, 20, "#CFE6CA", "#CFE6CA")
        rF.margin_right = 10
        rF.margin_left = 10
        ts.legend.add_face(rF, 0)
        ts.legend.add_face(TextFace(" Nucleotide based alignment"), 1)
        ts.legend_position = 3
 

    try:
        tree.set_species_naming_function(spname)
        annotate_tree_with_ncbi(tree)
        a = tree.search_nodes(species='Dictyostelium discoideum')[0]
        b = tree.search_nodes(species='Chondrus crispus')[0]
        #out = tree.get_common_ancestor([a, b])
        #out = tree.search_nodes(species='Haemophilus parahaemolyticus')[0].up
        tree.set_outgroup(out)    
        tree.swap_children()
    except Exception:
        pass
    
    tree.render(outfile, tree_style=ts, w=170, units='mm', dpi=150)
    tree.render(outfile+'.svg', tree_style=ts, w=170, units='mm', dpi=150)
    tree.render(outfile+'.pdf', tree_style=ts, w=170, units='mm', dpi=150)
Exemplo n.º 8
0
def draw(t, draw_alg=True):    
    def ly(node):
        node.img_style['vt_line_width'] = 1
        node.img_style['hz_line_width'] = 1
        if node.is_leaf():
            add_face_to_node(TextFace(' (%s)' %node.name.split()[0].replace("/exon2", ""), fsize=10, fgcolor='slategrey', tight_text=False), node, 1, position='branch-right')
            add_face_to_node(TextFace(node.species, fsize=12, fgcolor='black', fstyle='italic', tight_text=False), node, 0, position='branch-right')
            c = 1
            for tname in tracked_clades:
                if tname in node.named_lineage:
                    linF = TextFace(tname, fsize=10, fgcolor='white')
                    linF.margin_left = 3
                    linF.background.color = lin2color[tname]

                    add_face_to_node(linF, node, c, position='aligned')
                    c += 1
            for n in xrange(1, 20-(c-1)):
                add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'), node, c, position='aligned')
                c+=1

            if draw_alg and 'sequence' in node.features:
                #seqFace = SequenceFace(node.sequence,"aa",13)
                seqFace = SeqMotifFace(node.sequence, [])
                # [10, 100, "[]", None, 10, "black", "rgradient:blue", "arial|8|white|domain Name"],
                motifs = []
                last_lt = None
                for c, lt in enumerate(node.sequence):
                    if lt != '-':
                        if last_lt is None:
                            last_lt = c

                        if c+1 == len(node.sequence):
                            start, end = last_lt, c
                            w = end-start
                            motifs.append([start, end, "[]", w, 13, "slategrey", "slategrey", None])
                            last_lt = None

                    elif lt == '-':
                        if last_lt is not None:
                            start, end = last_lt, c-1
                            w = end-start
                            motifs.append([start, end, "[]", w, 13, "slategrey", "slategrey", None])
                            last_lt = None

                if not motifs:
                    print node, node.sequence

                seqFace = SeqMotifFace(node.sequence, motifs,
                                       intermotif_format="line",
                                       seqtail_format="line", scale_factor=1)
                add_face_to_node(seqFace, node, 20, aligned=True)

        else:
            if node.up: 
                add_face_to_node(TextFace('% 3g' %node.support, fsize=11, fgcolor='indianred'), node, 0, position='branch-top')
                
            if hasattr(node, "support2") and node.up:
                add_face_to_node(TextFace('% 3g' %float(node.support2), fsize=11, fgcolor='steelblue'), node, 0, position='branch-bottom')

        node.img_style['size'] = 0
        node.img_style['hz_line_color'] = 'black'
        node.img_style['vt_line_color'] = 'black'

    colors = random_color(num=len(tracked_clades))
    lin2color = dict([(ln, colors[i]) for i, ln in enumerate(tracked_clades)])
    ts = TreeStyle()
    ts.draw_aligned_faces_as_table = False
    ts.draw_guiding_lines = False
    ts.show_leaf_name = False
    ts.show_branch_support = False
    ts.layout_fn = ly
    print 'Rendering tree.pdf'
    t.render('tree.svg', tree_style=ts)
    t.render('tree.png', tree_style=ts)