thank you, that's what I was looking for!
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.
2 answers
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)
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
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.
Nice! Haven't used get_topology. Thanks.
Log in to answer this question.