如何使用 ETE Toolkit 更改节点标签上的字体方向 (Python)

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

我想更改 ETE Toolkit 生成的输出图像上的字体方向:http://etetoolkit.org

由于某些原因,旋转和方向更改不会影响标签,如下图所示:

在 Jupiter notebook 上生成此示例的代码如下:

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)

我还想知道是否可以将输出拆分为多行而不是一行?长序列往往占用很大的宽度 space 因此将序列拆分为多行是可行的。

来自 etetoolkit 邮件列表的回答:

1) 可以使用自定义布局和 TextFaces 进行旋转。 检查这个例子:

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)

2) 分成几行:我不完全理解你的问题,但是,如果询问减少最终树图像的宽度,唯一的方法是将绘图分成多个渲染调用,一个要绘制的节点。 例如。要将一棵树分成两个独立的图像,您可以 运行:

t.children[0].render("left_side.png")
t.children[1].render("right_side.png")

3) 你是说底部的分支刻度? 您可以设置 TreeStyle.show_scale=False