简体   繁体   English

ETE2:向节点添加图像

[英]ETE2: adding image to nodes

I have been trying to add different images to nodes in a phylogenetic tree using the ete2 software in Python, but have no success. 我一直在尝试使用Python中的ete2软件向系统发生树中的节点添加不同的图像,但没有成功。

from ete2 import Tree, TreeStyle, NodeStyle, TextFace, faces, add_face_to_node, AttrFace
ts.show_leaf_name = True
ts.show_branch_support = True

nw = """
(((Dre:0.008339,Dme:0.300613)1.000000:0.596401,
(Cfa:0.640858,Hsa:0.753230)1.000000:0.182035)1.000000:0.106234,
((Dre:0.271621,Cfa:0.046042)1.000000:0.953250,
(Hsa:0.061813,Mms:0.110769)1.000000:0.204419)1.000000:0.973467);
"""
t = Tree(nw)


img_path = "/home/leonard/Desktop/img_faces/"

humanFace = faces.ImgFace(img_path+"human.png")

mouseFace = faces.ImgFace(img_path+"mouse.png")


def my_layout(node):

 if name.startswith("Dre"):

  faces.add_face_to_node(humanface, node, column=1)

t.show(my_layout)

ts = TreeStyle()

t.render("img_faces.png", w=600, tree_style = ts)

These are error messages that I have been getting: 这些是我收到的错误消息:

 File "abc1.py", line 34, in <module>
    t.show(my_layout)
  File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/coretype/tree.py", line 1283, in show
    drawer.show_tree(self, layout=layout, tree_style=tree_style)
  File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/treeview/drawer.py", line 84, in show_tree
    tree_item, n2i, n2f = render(t, img)
  File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/treeview/qt4_render.py", line 258, in render
    set_style(n, layout_fn)
  File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/treeview/qt4_render.py", line 746, in set_style
    layout_func(n)
  File "abc1.py", line 29, in my_layout
    if name.startswith("Dre"):
NameError: global name 'name' is not defined

Any help is much appreciated! 任何帮助深表感谢!

This is the solution from Jaime Huerta Cepas in google group, and I quote: 这是来自谷歌小组的Jaime Huerta Cepas的解决方案,我引述:

"There is a general Python programming error in your script ("name" variable does not exist). I guess that what you meant is "node.name.startswith()" instead of "name.startswith()"" “你的脚本中存在一般的Python编程错误(”name“变量不存在)。我猜你的意思是”node.name.startswith()“而不是”name.startswith()“”

It worked. 有效。

According to the documentation ( ete2 ) you need to create a faces ready to read the name attribute of nodes. 根据文档( ete2 ),您需要创建一个准备好读取节点的name属性的面。

Try to add the following to your code: 尝试将以下内容添加到您的代码中:

#nameFace = faces.TextFace(open("text").readline().strip(), fsize=20, fgcolor="#009000")
nameFace = faces.AttrFace("name", fsize=20, fgcolor="#009000")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM