Phylo BioPython构建树木 [英] Phylo BioPython building trees

查看:117
本文介绍了Phylo BioPython构建树木的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用BioPython Phylo模块构建一棵树.
到目前为止,我所做的就是这张图片:

I trying to build a tree with BioPython, Phylo module.
What I've done so far is this image:

每个名称都有一个四位数的数字,后跟-和一个数字:该数字是指表示序列的次数.这意味着1578-22,该节点应代表22个序列.

each name has a four digit number followed by - and a number: this number refer to the number of times that sequence is represented. That means 1578 - 22, that node should represent 22sequences.

文件的序列已对齐:文件
带有构建树的距离的文件:文件

the file with the sequences aligned: file
the file with the distance to build a tree: file

所以现在我知道如何更改节点的每个大小.每个节点的大小都不同,这很容易将不同值组成一个数组:

So now I known how to change each size of the node. Each node has a different size, this is easy doing an array of the different values:

    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())

但是数组是任意的,我想将正确的节点大小放入正确的节点中,我尝试过:

But the array is arbitrary, I would like to put the correct node size into the right node, I tried this:

         for elem in list_size.keys():
             if labels == elem:
                 Phylo.draw_graphviz(tree_xml, prog="neato", node_size=a)

但是当我使用if语句时什么也没出现.

but nothing appears when I use the if statement.

不管这样做吗?

我真的很感激!

谢谢大家

推荐答案

我终于成功了.基本前提是您将使用labels/nodelist来构建node_sizes.这样,它们就可以正确关联.我确定我缺少一些使树看起来100%的重要选项,但看来节点大小显示正确.

I finally got this working. The basic premise is that you're going to use the labels/nodelist to build your node_sizes. This way they correlate properly. I'm sure I'm missing some important options to make the tree look 100% but it appears the node sizes are showing up properly.

#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()

结果树

这篇关于Phylo BioPython构建树木的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆