使用biopython写入fasta文件时出错 [英] Error while writing fasta file using biopython

查看:78
本文介绍了使用biopython写入fasta文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将fasta序列写入文件.

I used the following code to write the fasta sequence into file.

from Bio import SeqIO
sequences = "KKPPLLRR" # add code here
output_handle = open("example.fasta", "w")
SeqIO.write(sequences, output_handle, "fasta")
output_handle.close()

我遇到以下错误:

self = <Bio.SeqIO.FastaIO.FastaWriter object at 0x21c1d10>, record = 'M'
def write_record(self, record):
    """Write a single Fasta record to the file."""
    assert self._header_written
    assert not self._footer_written
    self._record_written = True

    if self.record2title:
        title = self.clean(self.record2title(record))
    else:

      id = self.clean(record.id)
    AttributeError: 'str' object has no attribute 'id'

有人可以提供针对此错误的解决方案吗?

Can somebody provide a solution for this error?

推荐答案

该错误告诉您 sequences 不是一组 SeqRecord 对象.您不能将一些字符串传递给 SeqIO.write .那就是应该怎么做:

That error tells you that sequences is not a set of SeqRecord objects. You cannot pass some string to the SeqIO.write. That's how it should be done:

from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import IUPAC


sequences = []
record = SeqRecord(Seq("KKPPLLRR", IUPAC.protein), id="My_protein")
sequences.append(record)

现在,您可以将序列传递给 SeqIO.write .如果序列很多,则可以创建一些生成器,这些生成器可以传递给 SeqIO.write :

Now you can pass sequences to the SeqIO.write. If you have a lot of sequences, you can create some generator that can be passed to SeqIO.write:

def generator_of_sequences():
    for string_seq in some_source_of_sequences:
        yield SeqRecord(Seq(string_seq, IUPAC.protein), id="Some_ID")

SeqIO.write(generator_of_sequences(), output_handle, "fasta")

这篇关于使用biopython写入fasta文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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