How to change the font orientation on node labels with ETE Toolkit (Python)

868 views Asked by At

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.

1

There are 1 answers

0
jhc On BEST ANSWER

answer from the etetoolkit mailing list:

1) rotation is possible by using custom layouts and TextFaces. Check this example:

from ete3 import Tree, TreeStyle, add_face_to_node, TextFace

from random import randint

def rotation_layout(node):
    if node.is_leaf():
        F = TextFace(node.name, tight_text=True)
        F.rotation = randint(0, 360)
        add_face_to_node(TextFace("third" ), node, column=8, position="branch-right")
        add_face_to_node(TextFace("second" ), node, column=2, position="branch-right")
        add_face_to_node(F, node, column=0, position="branch-right")

        F.border.width = 1
        F.inner_border.width = 1

def get_example_tree():
    t = Tree()
    t.populate(10)
    ts = TreeStyle()
    ts.rotation = 45
    ts.show_leaf_name = False
    ts.layout_fn = rotation_layout

    return t, ts

if __name__ == "__main__":
    t, ts = get_example_tree()
    t.show(tree_style=ts)

rotated tree labels

2) split on several lines: I don't fully understand your question but, if asking about reducing the width of the final tree image, the only way would be to split the 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")
t.children[1].render("right_side.png")

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