How to color the branches of a tree?

671 views
Skip to first unread message

jeama...@gmail.com

unread,
Oct 7, 2016, 2:34:55 PM10/7/16
to The ETE toolkit

I have a big phylogeny and would like to color the branches to easily detect clusters in my data. Something like what is depicted in phylogeny1.png. Notice that the branches are colored until the most recent common ancestor shared by individuals in the same cluster. The rest of the tree is plotted in black and I would like to keep that characteristic. 


I was working upon an old example published in this same forum (https://groups.google.com/forum/#!topic/etetoolkit/6Ceb4Mlmj5w), in which the tips of the tree are coloured, but didn't went to far. Is it possible to do such thing with ETE? To go from Tree1 (code included below) to Tree2? 


#!/bin/python3

from ete3 import Tree, TreeStyle, NodeStyle, AttrFace
# init colors, note capitals: http://etetoolkit.org/docs/latest/reference/reference_treeview.html#color-names
colors
= {'a':'Red', 'b':'Green', 'c':'Green', 'd':'Green', 'e':'Orange', 'f':'LightGreen'}
# init a newick string
newick
= "(a:0.2,(b:0.1,((c:0.8,d:0.4),(e:0.5,f:0.1))));"
# init a tree
t
= Tree(newick)
# iterate over tree leaves only
for l in t.iter_leaves():
   
# create a new label with a color attribute
    N
= AttrFace("name", fgcolor=colors[l.name])
   
# label margins
    N
.margin_top = N.margin_bottom = N.margin_left = 4.0
   
# labels aligned to the same level
    l
.add_face(N, 1, position='aligned')
   
# optionally colorize nodes too
    ns
= NodeStyle()
    ns
["fgcolor"] = colors[l.name]
    l
.img_style = ns
# init tree style
ts
= TreeStyle()
# remove default labels
ts
.show_leaf_name = False
# render image on notebook
t
.render("plot1.png", tree_style=ts)



phylogeny1.png
Tree2.png
Tree1.png

Jaime Huerta Cepas

unread,
Oct 7, 2016, 3:02:50 PM10/7/16
to eteto...@googlegroups.com
Hi, 
you can get something pretty close, but vertical lines cannot use two different colors, only one. 
For each node, you can set a couple of properties:
node.img_style['hz_line_color'] = 'red' # horizontal line color (there is also hz_line_type)
node.img_style['vt_line_color'] = 'red' # vertical line color

In your code, note that it is not necessary that you create a NodeStyle instance. 
You can just access node.img_style and the style will be initialized automatically: 

hope it helps, 
-jaime




--
You received this message because you are subscribed to the Google Groups "The ETE toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to etetoolkit+unsubscribe@googlegroups.com.
To post to this group, send email to eteto...@googlegroups.com.
Visit this group at https://groups.google.com/group/etetoolkit.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages