Phylo BioPython建筑树
题
我试图建立与BioPython,Phylo模块一棵树。结果 我到目前为止所做的就是这一形象:
每个名字具有四位数字后跟 - 和一个数字:这个数目指的是序列表示的次数。这意味着1578 - 22,该节点应该代表22sequences
与所述序列文件对准:文件结果 随着距离的文件建立一个树:文件
所以,现在我知道如何到节点的每个尺寸改变。每个节点具有不同的尺寸,这是很容易做的不同的值的数组:
fh = open(MEDIA_ROOT + "groupsnp.txt")
list_size = {}
for line in fh:
if '>' in line:
labels = line.split('>')
label = labels[-1]
label = label.split()
num = line.split('-')
size = num[-1]
size = size.split()
for lab in label:
for number in size:
list_size[lab] = int(number)
a = array(list_size.values())
但是,阵列是任意的,我想放正确的节点尺寸为右节点,我试图这样:
for elem in list_size.keys():
if labels == elem:
Phylo.draw_graphviz(tree_xml, prog="neato", node_size=a)
但没有出现,当我使用if语句。
反正这样做的?
我真的很感激!
谢谢大家
解决方案
我终于得到了这个工作。基本的前提是,你要使用labels/nodelist
建立你node_sizes
。这样,他们正确关联。我敢肯定,我错过了一些重要的选项,使树看起来100%,但它出现的节点尺寸是否正确显示出来。
#basically a stripped down rewrite of Phylo.draw_graphviz
import networkx, pylab
from Bio import Phylo
#taken from draw_graphviz
def get_label_mapping(G, selection):
for node in G.nodes():
if (selection is None) or (node in selection):
try:
label = str(node)
if label not in (None, node.__class__.__name__):
yield (node, label)
except (LookupError, AttributeError, ValueError):
pass
kwargs={}
tree = Phylo.read('tree.dnd', 'newick')
G = Phylo.to_networkx(tree)
Gi = networkx.convert_node_labels_to_integers(G, discard_old_labels=False)
node_sizes = []
labels = dict(get_label_mapping(G, None))
kwargs['nodelist'] = labels.keys()
#create our node sizes based on our labels because the labels are used for the node_list
#this way they should be correct
for label in labels.keys():
if str(label) != "Clade":
num = label.name.split('-')
#the times 50 is just a guess on what would look best
size = int(num[-1]) * 50
node_sizes.append(size)
kwargs['node_size'] = node_sizes
posi = networkx.pygraphviz_layout(Gi, 'neato', args='')
posn = dict((n, posi[Gi.node_labels[n]]) for n in G)
networkx.draw(G, posn, labels=labels, node_color='#c0deff', **kwargs)
pylab.show()
产生的树
不隶属于 StackOverflow