简体   繁体   English

ete2如果节点是一对坐标,如何操作

[英]ete2 how to operate if a node is a pair of coordinates

I need to store and then operate (add new nodes, search through, etc) a tree where every node is a pair of x,y coordinates. 我需要存储然后操作(添加新节点,搜索等)一棵树,其中每个节点都是一对x,y坐标。 I found ete2 module to work with trees, but I can't catch how to save a node as a tuple or list of coordinates. 我发现ete2模块可与树配合使用,但无法捕捉如何将节点另存为元组或坐标列表。 Is it possible with ete2? ete2是否可能?

Edit: 编辑:

I followed the tutorial here http://pythonhosted.org/ete2/tutorial/tutorial_trees.html#trees To create a simple tree: 我在这里遵循了该教程, 网址http://pythonhosted.org/ete2/tutorial/tutorial_trees.html#trees要创建简单的树:

t1 = Tree("(A:1,(B:1,(E:1,D:1):0.5):0.5);" )

where A, B, C is the name of a node and a number is a distance. 其中A,B,C是节点的名称,而数字是距离。

or 要么

t2 = Tree( "(A,B,(C,D));" )

I don't need names or distances, but a tree of tuples or lists, smth like: 我不需要名称或距离,而是一棵元组或列表的树,像这样:

t3 = Tree("([12.01, 10.98], [15.65, 12.10],([21.32, 6.31], [14.53, 10.86]));")

But the last input returns syntax error, in tutorials regarding ete2 I couldn't find any similar example. 但是最后一个输入返回语法错误,在有关ete2的教程中,我找不到任何类似的示例。 As a variant I think I could save coordinates as attributes, but attributes stored as strings. 作为一种变体,我认为我可以将坐标另存为属性,但将属性另存为字符串。 I need to operate with coordinates and it's tricky every time to traverse it from string to float and vice verse. 我需要使用坐标,每次将其从字符串移动到浮点,反之亦然,这很棘手。

You can annotate ete trees using any type of data. 您可以使用任何类型的数据注释ete树 Just give a name to every node, create a tree structure using such names, and annotate the tree with the coordinates. 只需为每个节点指定一个名称,使用这些名称创建树结构,然后用坐标注释树即可。

from ete2 import Tree

name2coord = {
'a': [1, 1], 
'b': [1, 1], 
'c': [1, 0], 
'd': [0, 1], 
}

# Use format 1 to read node names of all internal nodes from the newick string
t = Tree('((a:1.1, b:1.2)c:0.9, d:0.8);', format=1)     

for n in t.get_descendants():
   n.add_features(coord = name2coord[n.name])

# Now you can operate with the tree and node coordinates in a very easy way: 
for leaf in t.iter_leaves():
    print leaf.name, leaf.coord
# a [1, 1]
# b [1, 1]
# d [0, 1]

print t.search_nodes(coord=[1,0])
# [Tree node 'c' (0x2ea635)]

You can copy, save and restore annotated trees using pickle: 您可以使用pickle复制,保存和还原带注释的树:

t.copy('cpickle')
# or
import cPickle
cPickle.dump(t, open('mytree.pkl', 'w'))
tree = cPickle.load(open('mytree.pkl'))

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

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