首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将hcluster生成的ndarray转换为用于ete2包的Newick字符串

将hcluster生成的ndarray转换为用于ete2包的Newick字符串
EN

Stack Overflow用户
提问于 2012-02-21 00:28:57
回答 2查看 2.4K关注 0票数 7

我有一个通过运行以下命令创建的向量列表:

代码语言:javascript
复制
import hcluster
import numpy as np
from ete2 import Tree

vecs = [np.array(i) for i in document_list] 

其中document_list是我正在分析的web文档的集合。然后,我执行分层聚类:

代码语言:javascript
复制
Z = hcluster.linkage(vecs, metric='cosine') 

这将生成ndarray,例如:

代码语言:javascript
复制
[[ 12.          19.           0.           1.        ]
[ 15.          21.           0.           3.        ]
[ 18.          22.           0.           4.        ]
[  3.          16.           0.           7.        ]
[  8.          23.           0.           6.        ]
[  5.          27.           0.           6.        ]
[  1.          28.           0.           7.        ]
[  0.          21.           0.           2.        ]
[  5.          29.           0.18350472   2.        ]
[  2.          10.           0.18350472   3.        ]
[ 47.          30.           0.29289577   9.        ]
[ 13.          28.           0.29289577  13.        ]
[ 73.          32.           0.29289577  18.        ]
[ 26.          12.           0.42264521   5.        ]
[  5.          33.           0.42264521  12.        ]
[ 14.          35.           0.42264521  12.        ]
[ 19.          35.           0.42264521  18.        ]
[  4.          20.           0.31174826   3.        ]
[ 34.          21.           0.5         19.        ]
[ 38.          29.           0.31174826  21.        ]]

是否可以将此ndarray转换为可传递给ete2树()构造函数的newick字符串,以便我可以使用ete2提供的工具绘制和操作newick树?

尝试这样做有意义吗?如果没有,有没有其他方法可以使用相同的数据和ete2来生成树/树图(我意识到还有其他包可以绘制树状图,比如树状图和hcluster本身,但还是更喜欢使用ete2 )?

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2013-07-15 23:03:56

我使用下面的方法来做几乎相同的事情:

代码语言:javascript
复制
from hcluster import linkage, to_tree
from ete2 import Tree

#hcluster part
Y = dist_matrix(items, dist_fn)
Z = linkage(Y, "single")
T = to_tree(Z)

#ete2 section
root = Tree()
root.dist = 0
root.name = "root"
item2node = {T: root}

to_visit = [T]
while to_visit:
    node = to_visit.pop()
    cl_dist = node.dist /2.0
    for ch_node in [node.left, node.right]:
        if ch_node:
            ch = Tree()
            ch.dist = cl_dist
            ch.name = str(ch_node.id)
            item2node[node].add_child(ch)
            item2node[ch_node] = ch
            to_visit.append(ch_node)

# This is your ETE tree structure
tree = root
票数 4
EN

Stack Overflow用户

发布于 2017-03-22 21:19:44

更新:

代码语言:javascript
复制
from hcluster import linkage, to_tree
from ete2 import Tree

#hcluster part
Y = dist_matrix(items, dist_fn)
Z = linkage(Y, "single")

R,T       = to_tree( mat, rd=True )
#print "ROOT", R, "TREE", T
root      = Tree()
root.dist = 0
root.name = 'root'
item2node = {R.get_id(): root}
to_visit  = T

while to_visit:
    node = to_visit.pop()
    #print "NODE", node
    cl_dist = node.dist / 2.0

    for ch_node in [node.get_left(), node.get_right()]:
        if ch_node:
            ch_node_id         = ch_node.get_id()
            ch_node_name       = str(ch_node_id)
            ch                 = Tree()
            ch.dist            = cl_dist
            ch.name            = ch_node_name

            if nodeNames:
                if ch_node_id < len(nodeNames):
                    ch.name    = nodeNames[ ch_node_id ]

            item2node[ch_node_id] = ch
            item2node[ch_node_id].add_child(ch)
            to_visit.append(ch_node)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9364609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档