Howto render a tree with attributes in python toolkit ete?
2
1
Entering edit mode
9.3 years ago
whooli ▴ 10

I'm using ete toolkit to get a phylogenetic tree for a list of ncbi taxids:

from ete2 import NCBITaxa
ncbi = NCBITaxa()
tree = ncbi.get_topology([9606, 9598, 10090, 7707, 8782])
print tree.get_ascii(attributes=["sci_name", "rank"])

Printing it with ascii chars works, but how to render this tree including the attributes (sci_name, rank) to an image?

tree.render("tree.pdf")

There seems to be no 'attribute' in this function.

tree python ete • 5.0k views
ADD COMMENT
5
Entering edit mode
9.3 years ago
jhc ★ 3.0k

tree.render() is a general purpose method. As node attributes are completely arbitrary, you need to specify what should be drawn and where... Check the docs regarding the drawing system: https://pythonhosted.org/ete2/tutorial/tutorial_drawing.html

For your example, something like this should work:

from ete2 import NCBITaxa, AttrFace, TreeStyle
ncbi = NCBITaxa()
tree = ncbi.get_topology([9606, 9598, 10090, 7707, 8782])

# custom layout: adds "rank" on top of branches, and sci_name as tip names
def my_layout(node):
    if getattr(node, "rank", None):
        rank_face = AttrFace("rank", fsize=7, fgcolor="indianred")
        node.add_face(rank_face, column=0, position="branch-top")
    if node.is_leaf():
        sciname_face = AttrFace("sci_name", fsize=9, fgcolor="steelblue")
        node.add_face(sciname_face, column=0, position="branch-right")
ts = TreeStyle()
ts.layout_fn = my_layout
ts.show_leaf_name = False
tree.render("tree.png", tree_style=ts)
ADD COMMENT
0
Entering edit mode

thank you, that's what I was looking for!

ADD REPLY
1
Entering edit mode
9.3 years ago
AndreiR ▴ 50

Have you tried:

from ete2 import NCBITaxa
ncbi = NCBITaxa()
tree = ncbi.get_topology([9606, 9598, 10090, 7707, 8782]) 

ncbi.annotate_tree(tree,taxid_attr='name')

from https://pythonhosted.org/ete2/reference/reference_ncbi.html

ADD COMMENT
0
Entering edit mode

What annotate_tree does is to read a bare tree and add the rank, sci_name, taxid attrs automatically (by parsing taxids from tip names, for instance). It is very useful to annotate your own trees, but the tree objects returned by ncbi.get_topology() are already annotated.

ADD REPLY
0
Entering edit mode

Nice! Haven't used get_topology. Thanks.

ADD REPLY

Login before adding your answer.

Traffic: 1782 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6