将输出存储到 FASTA 文件

Storing the Output to a FASTA file

from Bio import SeqIO
from Bio import SeqRecord
from Bio import SeqFeature
for rec in SeqIO.parse("C:/Users/Siva/Downloads/sequence.gp","genbank"):
       if rec.features:
            for feature in rec.features:
                    if feature.type =="Region":
                          seq1 = feature.location.extract(rec).seq
                          print(seq1)
                          SeqIO.write(seq1,"region_AA_output1.fasta","fasta")

我正在尝试将输出写入 FASTA 文件,但出现错误。有谁能够帮助我? 这是我得到的错误

  Traceback (most recent call last):
  File "C:\Users\Siva\Desktop\region_AA.py", line 10, in <module>
  SeqIO.write(seq1,"region_AA_output1.fasta","fasta")
  File "C:\Python34\lib\site-packages\Bio\SeqIO\__init__.py", line 472, in      write
  count = writer_class(fp).write_file(sequences)
  File "C:\Python34\lib\site-packages\Bio\SeqIO\Interfaces.py", line 211, in write_file
  count = self.write_records(records)
  File "C:\Python34\lib\site-packages\Bio\SeqIO\Interfaces.py", line 196, in write_records
  self.write_record(record)
  File "C:\Python34\lib\site-packages\Bio\SeqIO\FastaIO.py", line 190, in write_record
  id = self.clean(record.id)

AttributeError: 'str' 对象没有属性 'id'

首先,您尝试将普通序列编写为 fasta 记录。一条 fasta 记录由一个序列加上一个 ID 行(以“>”开头)组成。您还没有提供 ID,所以 fasta 作者没有什么可写的。您应该写入整个记录,或者通过自己添加一个 ID 将序列变成一个 fasta 记录。

其次,即使您的方法写入了任何内容,它也会不断地将每条新记录覆盖到同一个文件中。您最终只会得到文件中的最后一条记录。

一种更简单的方法是将所有内容存储在一个列表中,然后在完成循环后写入整个列表。例如:

new_fasta = []
for rec in SeqIO.parse("C:/Users/Siva/Downloads/sequence.gp","genbank"):
    if rec.features:
        for feature in rec.features:
            if feature.type =="Region":
                seq1 = feature.location.extract(rec).seq
                # Use an appropriate string for id 
                new_fasta.append('>%s\n%s' % (rec.id, seq1))  

with open('region_AA_output1.fasta', 'w') as f:
    f.write('\n'.join(new_fasta))