将对齐和ascii树写入文件

时间:2014-10-12 19:18:18

标签: python file-io biopython

我使用AlignIO库的PhyloBiopython函数来对齐多个序列并创建系统发育树。在这方面文件非常清楚,我在获得结果方面没有任何问题。但是我不想只在程序运行时显示结果,而是想将它们保存到文件中(最好是不同的文件)。但是怀疑我得到了TypeError: expected a character buffer object。我无法在SO或通过谷歌找到任何帮助将这些结果写入文件。

我的代码:

from Bio import AlignIO, Phylo
align = AlignIO.read("allseqs.aln", "clustal")

alignfile = open("alignfile.txt","w")
print(align)
#alignfile.write(align) -->gives error

tree = Phylo.read("allseqs.dnd","newick")
Phylo.draw_ascii(tree)

align结果如下所示:

SingleLetterAlphabet() alignment with 14 rows and 1688 columns
--------------------------------------------...--- M.HpyFIII.dna
--------------------------------------------...--- M.HpyFIV.dna
TTGCATAAAGTTTTTATTATGGAAGCTTTGGAATGTTTGAAAAG...--- M.HpyFII.dna
--------------------------------------------...--- M.HpyFXII.dna
ATGCCTTCAAACGCTCTTTCTATTGAAGAAATCGCTCGCCTAGT...TAA M.HpyFVII.dna
--------------------------------------------...--- M.HpyFV.dna
--------------------------------------------...--- M.HpyFXI.dna
--------------------------------------------...--- M.HpyFORFX.dna
--------------------------------------------...--- M1.HpyFXIII.dna
--------------------------------------------...--- M.HpyFVI.dna
---------------------------------------ATGCA...--- M.HpyFVIII.dna
--------------------------------------------...--- M.HpyFIX.dna
--------------------------------------------...--- M2.HpyFXIII.dna
--------------------------------------------...--- M.HpyFI.dna

系统发育树看起来像这样:

                                          __________________ M.HpyFI.dna
                                         |
                                         |__________________ M2.HpyFXIII.dna
                                         |
                                         |__________________ M.HpyFII.dna
                                         |
                                         |        __________ M.HpyFIII.dna
                                         |_______|
                                         |       |__________ M.HpyFIV.dna
                                         |
                                         |  ________________ M.HpyFVII.dna
                                         |_|
                                         | |________________ M.HpyFXII.dna
_________________________________________|
                                         |__________________ M.HpyFIX.dna
                                         |
                                         |    ______________ M.HpyFVI.dna
                                         |___|
                                         |   |______________ M.HpyFVIII.dna
                                         |
                                         | __________________ M.HpyFORFX.dna
                                         ,|
                                         ||   _____________ M.HpyFV.dna
                                         ||__|
                                         |   |______________ M.HpyFXI.dna
                                         |
                                         |__________________ M1.HpyFXIII.dna

编辑代码:

from Bio import AlignIO, Phylo,SeqIO
import networkx, pylab

align = AlignIO.read("allseqs.aln", "clustal")
align_write = SeqIO.write(align,"alignment_file.fasta","fasta")

tree = Phylo.read("allseqs.dnd","newick")
with open('phylo_tree.txt', 'wb') as fh:
     Phylo.draw_ascii(tree, file = fh)

1 个答案:

答案 0 :(得分:0)

全部在the documentations,正如@jonrsharpe所提到的

with open('asciout.txt', 'wb') as fh:
    Phylo.draw_ascii(tree, file = fh)

with open("alignfile.txt","w") as alignfile:
    AlignIO.write(align, alignfile, format)

或者如果你真的想要字符串表示:

with open("alignfile.txt","w") as alignfile:
    alignfile.write(str(align))

str(align)print得到的。