[英]ete2 how to operate if a node is a pair of coordinates
我需要存儲然后操作(添加新節點,搜索等)一棵樹,其中每個節點都是一對x,y坐標。 我發現ete2模塊可與樹配合使用,但無法捕捉如何將節點另存為元組或坐標列表。 ete2是否可能?
編輯:
我在這里遵循了該教程, 網址為http://pythonhosted.org/ete2/tutorial/tutorial_trees.html#trees要創建簡單的樹:
t1 = Tree("(A:1,(B:1,(E:1,D:1):0.5):0.5);" )
其中A,B,C是節點的名稱,而數字是距離。
要么
t2 = Tree( "(A,B,(C,D));" )
我不需要名稱或距離,而是一棵元組或列表的樹,像這樣:
t3 = Tree("([12.01, 10.98], [15.65, 12.10],([21.32, 6.31], [14.53, 10.86]));")
但是最后一個輸入返回語法錯誤,在有關ete2的教程中,我找不到任何類似的示例。 作為一種變體,我認為我可以將坐標另存為屬性,但將屬性另存為字符串。 我需要使用坐標,每次將其從字符串移動到浮點,反之亦然,這很棘手。
您可以使用任何類型的數據注釋ete樹 。 只需為每個節點指定一個名稱,使用這些名稱創建樹結構,然后用坐標注釋樹即可。
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)]
您可以使用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.