AttributeError:使用BioPython解析'str'对象没有属性'id'时,fasta

编程入门 行业动态 更新时间:2024-10-25 17:25:58
本文介绍了AttributeError:使用BioPython解析'str'对象没有属性'id'时,fasta的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Bio和SeqIO打开包含多个序列的FASTA文件,编辑序列名称以在所有名称的末尾删除".seq",(> SeqID20.seq应该变为> SeqID20),然后将所有序列写入新的FASTA文件,但是出现以下错误

I am trying to use Bio and SeqIO to open a FASTA file that contains multiple sequences, edit the names of the sequences to remove a '.seq' on the end of all the names, (>SeqID20.seq should become >SeqID20), then write all the sequences to a new FASTA file, But i get the following error

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")

但我也尝试过此操作,并得到相同的错误:

but i have also tried this and get the same error:

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')

我认为从列表"pair"到写入新文件时出现了一些错误,但是我不确定要更改什么.任何帮助将不胜感激!

I assume I'm making some error in going from my list 'pair' to writing to a new file, but I'm not sure what to change. Any help would be appreciated!

推荐答案

您的错误发生是因为SeqIO.write接受SeqRecord或SeqRecord s的列表/迭代器,但是您只将其添加为类似于.相反,我建议您只修改SeqRecord .id和.description(请注意,如果标题行中有whitepace,则也需要进行处理).同样,一次写入所有记录,而不是在每次迭代中调用.write,都是最有效的方式(跨Biopython版本):

Your error occurs because SeqIO.write accepts a SeqRecord or a list/iterator of SeqRecords but you are feeding it just a list like [name, sequence]. Instead I suggest you just modify the SeqRecord .id and .description (note, if there is whitepace in the header line, you'll need to handle this too). Also it is most efficient (across Biopython versions) to write all the records at once, rather than calling .write each iteration:

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')

更多推荐

AttributeError:使用BioPython解析'str'对象没有属性'id'时,fasta

本文发布于:2023-10-22 08:20:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1516967.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   BioPython   AttributeError   fasta

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!