This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Biopython: Is It Possible To Update Only Sequence In A Seqrecord Object?

Well I know sequence objects are immutable, but why cant we assign a new sequence to a SeqRecord object without changing any other data.

biopython

If you are hitting an error relating to letter annotations (e.g. "You must empty the letter annotations first!"), you can copy them to a temporary dict object while you modify the seq, clear them, and then copy them back to the SeqRecord afterwards.

1 answer

Yep:

from Bio import Seq
from Bio import SeqRecord

rec = SeqRecord.SeqRecord(id='test', description='', seq=Seq.Seq('ATTGA'))
print rec.format('fasta')

#>test
#ATTGA

rec.seq = Seq.Seq('ATTGG')
print rec.format('fasta')

#>test
#ATTGG

I just wanted to post something similar - it's important that the new sequence 'ATTGG' is an object of class Seq, not a string, because then you'll get (at least on my system with Python 2.7.5 and BioPython 1.62b)

SeqRecord (id=test) has an invalid sequence.

Log in to answer this question.