将输出存储到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文件,但出现错误.有谁能够帮我? 这是我得到的错误
I am trying to write the output to a FASTA file but i am getting error. Can anybody help me? This the error which i got
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'
AttributeError: 'str' object has no attribute 'id'
推荐答案
首先,您尝试编写一个普通序列作为fasta记录. Fasta记录包含一个序列和一个ID行(以>"开头).您尚未提供ID,因此Fasta编写器没有任何内容可写.您应该写完整的记录,或者通过自己添加一个ID将序列变成快速记录.
First, you're trying to write a plain sequence as a fasta record. A fasta record consists of a sequence plus an ID line (prepended by ">"). You haven't provided an ID, so the fasta writer has nothing to write. You should either write the whole record, or turn the sequence into a fasta record by adding an ID yourself.
第二,即使您的方法编写了任何内容,它也会不断将每个新记录覆盖到同一文件中.您最终只会得到文件中的最后一条记录.
Second, even if your approach wrote anything, it's continually overwriting each new record into the same file. You'd end up with just the last record in the file.
一种更简单的方法是将所有内容存储在列表中,然后在完成循环后写出整个列表.例如:
A simpler approach is to store everything in a list, and then write the whole list when you're done the loop. For example:
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))
这篇关于将输出存储到FASTA文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!