[英]Phylo BioPython building trees
我試圖用BioPython,Phylo模塊構建一棵樹。
到目前為止我所做的是這張圖片:
每個名稱都有一個四位數字后跟 - 和一個數字:這個數字指的是表示序列的次數。 這意味着1578 - 22,該節點應代表22個序列。
所以現在我知道如何更改節點的每個大小。 每個節點都有不同的大小,這很容易做一個不同值的數組:
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()
結果樹
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.