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

2024-05-14 04:13:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

ETE

在Jupiter笔记本上生成此示例的代码如下:

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)

我还想知道是否可以将输出拆分为多行而不是一行?长序列往往占用巨大的宽度空间,因此在多行上进行序列分割是可行的。在


Tags: org图像treehttp字体序列工具箱方向
1条回答
网友
1楼 · 发布于 2024-05-14 04:13:49

来自etetoolkit邮件列表的答案:

1)可通过使用自定义布局和文本面进行旋转。 检查此示例:

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)分成几行:我不完全理解你的问题,但是,如果问到如何减少最终树图像的宽度,唯一的方法就是将绘图拆分为多个渲染调用,每个要绘制的节点一个。 例如。要将一棵树拆分为两个独立的图像,可以运行:

^{pr2}$

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

相关问题 更多 >