Biopython系统发生树在SVG文件中编辑标签

时间:2015-10-15 11:44:47

标签: python svg biopython phylogeny

我尝试使用python库Biopython和Phylo.draw()将全长标签放入我的系统发育树中。

我导入了我的newick文件格式,我绘制它,然后保存它:

from Bio import Phylo
import pylab
f = 'path/to/my/file'
tree = Phylo.read(file, 'newick')
tree.ladderize()
Phylo.draw(tree, do_show=False)
pylab.axis("off")
pylab.savefig("tree2.svg",format='svg', bbox_inches='tight', dpi=300)

但问题是Phylo.draw将标签切成40个字母的标签。

我可以用这个组合显示全长标签:

for leaf in tree.get_terminals(): 
    print leaf.name

我看到了代码源,我看了文档,我没有找到这个函数如何剪切长标签,或者如何用全长标签替换剪切标签,我看到它们是选项{{1} }  或label_func=str但我不知道如何使用它。

您可以将数据粘贴到文本文件中进行测试:

branch_labels=None

编辑: 评论后我编辑标题,将“png”替换为“svg”,隐藏轴 并添加图片。

enter image description here

1 个答案:

答案 0 :(得分:2)

你快到了!您需要指定label_func参数,这是一个函数。像这样:

from Bio import Phylo
import pylab

def get_label(leaf):
    return leaf.name

f = 'path/to/my/file'
tree = Phylo.read(f, 'newick')
tree.ladderize()
Phylo.draw(tree, label_func=get_label, do_show=False)
pylab.axis('off')
pylab.savefig('tree2.svg',format='svg', bbox_inches='tight', dpi=300)
相关问题