属性错误:';str';对象没有属性';id';使用BioPython解析fasta
我试图使用Bio和SeqIO打开一个包含多个序列的FASTA文件,编辑序列的名称以删除所有名称末尾的“.seq”(>SeqID20.seq应变成>SeqID20),然后将所有序列写入一个新的FASTA文件,但我得到以下错误属性错误:';str';对象没有属性';id';使用BioPython解析fasta,python,python-3.x,bioinformatics,biopython,fasta,Python,Python 3.x,Bioinformatics,Biopython,Fasta,我试图使用Bio和SeqIO打开一个包含多个序列的FASTA文件,编辑序列的名称以删除所有名称末尾的“.seq”(>SeqID20.seq应变成>SeqID20),然后将所有序列写入一个新的FASTA文件,但我得到以下错误 AttributeError: 'str' object has no attribute 'id' 这是我开始的: with open ('lots_of_fasta_in_file.fasta') as f: for seq_record in SeqIO.pa
AttributeError: 'str' object has no attribute 'id'
这是我开始的:
with open ('lots_of_fasta_in_file.fasta') as f:
for seq_record in SeqIO.parse(f, 'fasta'):
name, sequence = seq_record.id, str(seq_record.seq)
pair = [name.replace('.seq',''), sequence]
SeqIO.write(pair, "new.fasta", "fasta")
但我也尝试过这个,得到了同样的错误:
file_in ='lots_of_fasta_in_file.fasta'
file_out='new.fasta'
with open(file_out, 'w') as f_out:
with open(file_in, 'r') as f_in:
for seq_record in SeqIO.parse(f_in, 'fasta'):
name, sequence = seq_record.id, str(seq_record.seq)
# remove .seq from ID and add features
pair = [name.replace('.seq',''), sequence]
SeqIO.write(pair, file_out, 'fasta')
我假设我在从列表“对”写入新文件时出错,但我不确定要更改什么。任何帮助都将不胜感激 不是针对您的代码的真正解决方案,而是针对您的需要:
sed 's/\.seq$//' lots_of_fasta_in_file.fasta > new.fasta
此脚本假定一个正确的fasta文件。它将删除任何行末尾的所有“.seq”字符串。在一个正确的fasta文件中,只有ID行应该包含这个字符。您的错误发生是因为
SeqIO.write
接受SeqRecord
或SeqRecord
的列表/迭代器,但您只向它提供了一个类似[name,sequence]
的列表。相反,我建议您只修改SeqRecord
.id
和.description
(注意,如果标题行中有空格,您也需要处理)。同时,一次写入所有记录是最有效的(跨Biopython版本),而不是调用。每次迭代写入:
from Bio import SeqIO
def yield_records():
with open('lots_of_fasta_in_file.fasta') as f:
for seq_record in SeqIO.parse(f, 'fasta'):
seq_record.id = seq_record.description = seq_record.id.replace('.seq','')
yield seq_record
SeqIO.write(yield_records(), 'new.fasta', 'fasta')
这将是有用的,知道这是失败的路线。。。这是名称,sequence=seq_record.id,str(seq_record.seq)
?知道这一点真是太好了!我有这个信息的其他用途,所以感谢评论!