AttributeError:使用BioPython解析'str'对象没有属性'id'时,fasta [英] AttributeError: 'str' object has no attribute 'id' using BioPython, parsing fasta

查看:124
本文介绍了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接受SeqRecordSeqRecord 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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