How to change the font orientation on node labels?

139 views
Skip to first unread message

Marko Manninen

unread,
Feb 22, 2016, 7:52:02 AM2/22/16
to The ETE toolkit
I asked this question jsut recently on stackoverflow.com. You might want to add ete-toolkit label there (if some users has enough points to create new labels) and follow related questions.

http://stackoverflow.com/questions/35551004/how-to-change-the-font-orientation-on-node-labels-with-ete-toolkit-python


I'd like to change the orientation of font faces on the output image that is produced by ETE Toolkit: http://etetoolkit.org


For some reason rotation and orientation change does not affect to labels as seen on the picture below:


ETE

Code to produce this example on the Jupiter notebook is following:


from ete3 import Tree, TreeStyle

def draw_ete(newick):
    t = Tree(newick)
    ts = TreeStyle()
    ts.scale = 5
    ts.rotation = -90
    ts.orientation = 1
    return t.render("%%inline", tree_style=ts)

newick = """((p, e, a, s, ((e, _), p, o), r, r, i, d, g, (e, _)), (h, o, t), ((,, ↵), (p, e, a, s, ((e, _), p, o), r, r, i, d, g, (e, _))), (c, (o, l, d)), ((,, ↵), (p, e, a, s, ((e, _), p, o), r, r, i, d, g, (e, _))), ((i, n), _, t, h, ((e, _), p, o), t, (,, ↵), n, (i, n), (e, _), d, a, y, s, _, (o, l, d), .), ↵, ↵, (s, o, m, (e, _), l, i, k, (e, _), i, t, _), (h, o, t), ((,, ↵), (s, o, m, (e, _), l, i, k, (e, _), i, t, _)), (c, (o, l, d)), ((,, ↵), (s, o, m, (e, _), l, i, k, (e, _), i, t, _)), ((i, n), _, t, h, ((e, _), p, o), t, (,, ↵), n, (i, n), (e, _), d, a, y, s, _, (o, l, d), .));"""
draw_ete(newick)


I would also like to know if it is possible to split the output to multiple rows instead of one line? Long sequences tend to take huge width space so it would be practical to get sequence split on several lines.


Third question is: how to remove |-----| {num} endin from the image?

Thanks in forehand!
-Marko

Jaime Huerta Cepas

unread,
Feb 22, 2016, 8:03:35 AM2/22/16
to eteto...@googlegroups.com
Hi Marko, 
1) rotation is possible by using custom layouts and TextFaces. 
which produces something similar to this image: http://etetoolkit.org/static/img/gallery/rotatedface400x400.png

2) split on several lines: I don't fully understand your questions. If asking about reducing the width of the final tree image, the only way would be to split de drawing in multiple render calls, one per node that you want to draw. 
For instance. To split one tree into two independent images, you could run: 
t.children[0].render("left_side.png")
and 
t.children[1].render("right_side.png")

3) You mean the branch scale at bottom?
You  can set TreeStyle.show_scale=False 

best, 
-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+...@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.

Marko Manninen

unread,
Feb 22, 2016, 8:56:05 AM2/22/16
to The ETE toolkit
Hi Jamie,

great timing!

1) I will try rotation with example you gave. I already tried rotation attribute but I have missed something...

2) If possible I'd like to get that rendered on a notebook. I suppose %%inline printer does not support this kind of multiple rendered images on a same cell behavior? Should I first save two independent images and then combine them as one with some image manipulation utility? I think you understood correctly what I'm looking for, but poorly could describe.

3) simple and working.

-Marko

Jaime Huerta Cepas

unread,
Feb 22, 2016, 9:02:19 AM2/22/16
to eteto...@googlegroups.com

2) If possible I'd like to get that rendered on a notebook. I suppose %%inline printer does not support this kind of multiple rendered images on a same cell behavior? Should I first save two independent images and then combine them as one with some image manipulation utility? I think you understood correctly what I'm looking for, but poorly could describe.

It should work the same with %%inline. 
But yes, I am afraid you will need to concatenate all partial images vertically (you can try to automate it with i.e. imagemagick ...)   
 

Marko Manninen

unread,
Feb 22, 2016, 11:48:17 AM2/22/16
to The ETE toolkit
Thanks for all help! I got it all working pretty well:

from ete3 import Tree, TreeStyle, NodeStyle, TextFace
from IPython.display import HTML

def tree_style(rotation):
    # define node/leaf modifier
    def rotate_labels(node):
        ns = NodeStyle()
        ns["size"] = 1
        if node.is_leaf():
            F = TextFace(node.name)
            F.rotation = rotation
            F.margin_right = F.margin_left = 2.0
            node.add_face(F, 0, position="aligned")
        node.img_style = ns
    
    # init tree style
    ts = TreeStyle()
    # scale of the figure
    ts.scale = 10
    # remove default labels
    ts.show_leaf_name = False
    # hide scale
    ts.show_scale = False
    # rotate 90 degress counterclockwise
    ts.rotation = rotation
    # orient tree from left to right
    ts.orientation = 1
    # rotate labels
    ts.layout_fn = rotate_labels
    return ts

def get_tree(newick_str):
    # init tree
    return Tree(newick_str)

def branches(newick_str, rotation = -90):
    # get tree instance
    t = get_tree(newick_str)
    # length of the tree
    l = len(t.children)
    # create inline display object from each tree branch
    return [t.children[i].render("%%inline", tree_style=tree_style(rotation)) for i in range(l)]

def main(newick_str):
    # iterate over all branches on a tree and base64 encode image data for the img tag
    imagesList = ''.join(['<div style="height:100px;float:left;"><img src="data:image/png;base64,%s" /></div>' 
                       % base64.b64encode(branch.data).decode() for branch in branches(newick_str)])
    # display on jupyter notebook cell
    return HTML(imagesList)

# init a newick string
newick_str = """((p, e, a, s, ((e, _), p, o), r, r, i, d, g, (e, _)), (h, o, t), ((,, ↵), (p, e, a, s, ((e, _), p, o), r, r, i, d, g, (e, _))), (c, (o, l, d)), ((,, ↵), (p, e, a, s, ((e, _), p, o), r, r, i, d, g, (e, _))), ((i, n), _, t, h, ((e, _), p, o), t, (,, ↵), n, (i, n), (e, _), d, a, y, s, _, (o, l, d), .), ↵, ↵, (s, o, m, (e, _), l, i, k, (e, _), i, t, _), (h, o, t), ((,, ↵), (s, o, m, (e, _), l, i, k, (e, _), i, t, _)), (c, (o, l, d)), ((,, ↵), (s, o, m, (e, _), l, i, k, (e, _), i, t, _)), ((i, n), _, t, h, ((e, _), p, o), t, (,, ↵), n, (i, n), (e, _), d, a, y, s, _, (o, l, d), .));"""
main(newick_str)

 Output is:

Auto Generated Inline Image 1
Auto Generated Inline Image 10
Auto Generated Inline Image 11
Auto Generated Inline Image 12
Auto Generated Inline Image 13
Auto Generated Inline Image 14
Auto Generated Inline Image 2
Auto Generated Inline Image 3
Auto Generated Inline Image 4
Auto Generated Inline Image 5
Auto Generated Inline Image 6
Auto Generated Inline Image 7
Auto Generated Inline Image 8
Auto Generated Inline Image 9

Jaime Huerta Cepas

unread,
Feb 22, 2016, 1:55:32 PM2/22/16
to eteto...@googlegroups.com
nice! thanks for sharing

--
Reply all
Reply to author
Forward
0 new messages