2010-10-29 60 views
6

我試圖用BioPython,Phylo模塊構建樹。
什麼我目前所做的就是這種形象:alt textPhylo BioPython構建樹

每個名字都有一個四位數字,其次是 - 和一些:這個數字指的倍序列代表的數字。這意味着1578-22,那個節點應該代表22個序列。

對齊序列文件:file

所以,現在我知道如何在節點的每個尺寸變化:file
與構建一棵樹的距離的文件。每個節點都有不同的大小,這是很容易做的不同值的數組:

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語句時什麼也沒有出現。

反正這麼做?

我真的很感激!

謝謝大家

+0

你能否提供你正在使用那棵樹的測試文件? – rwilliams 2010-11-02 08:50:07

回答

8

我終於得到了這個工作。基本前提是您要使用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() 

結果樹 alt text

+0

其實我也嘗試過,它也做同樣的事情。我可以提供測試文件,但可能它太大而無法在此處顯示 – pavid 2010-11-02 10:33:02

+0

嘗試Pastie.org並選擇html/xml作爲 – rwilliams 2010-11-02 10:41:14

+0

這個問題的類型。謝謝:) – pavid 2010-11-02 11:23:23