[Biopython] changing record attributes while iterating

Bala subramanian bala.biophysics at gmail.com
Tue Oct 4 08:05:36 UTC 2011


Friends,
I have a fasta file. I need to modify the record id by adding a suffix to
it. So i used SeqRecord (the code attached below). It is working fine but i
would like to know if there is any simple way to do that. ie. if i can
change the record attributes while iterating through the fasta with
SeqIO.parse itself. I tried something like following but i couldnt get what
i wanted.

new_list=[]
for record in SeqIO.parse(open(argv[1], "rU"), "fasta"):
                    record.id=record.id + '_suffix'
                    new_list.append(record)

Hence i used SeqRecord to do the modification ?
----------------------------------------------------------------------------------------------------
#!/usr/bin/env python
from Bio import SeqIO
from Bio.SeqRecord import SeqRecord
from Bio.Seq import Seq
from sys import argv

new_list=[]

for record in SeqIO.parse(open(argv[1], "rU"), "fasta"):

    seq=str(record.seq)
    newrec=SeqRecord(Seq(seq),id=record.id+"_suffix",name='',description='')

    new_list.append(newrec)

output_handle = open(raw_input('Enter the output file:'), 'w')
SeqIO.write(new_list, output_handle, "fasta")
output_handle.close()



More information about the Biopython mailing list