How to use seek() with Biopython SeqIO object
I would like to iterate fasta file again and again using Biopython SeqIO object. The python seek (0,0) function can do this. But I am not getting expected output when I used it with biopython SeqIO object. How can I iterate fasta file from the beginning using biopython SeqIO object?
• 3,602 views
•
link
1 answer
As far as I know you can't use seek on a file object what's you've made a SeqIO generator from it (or, you can but it won't change the generator). You have plenty of other options is you want to iterate over the records many times:
- Read the whole thing into memory as a list
- Use SeqIO.index and the itervalues() method of the resulting object
- Create a new generator every time you iterate:
handle = open("my.fasta", "r")
[rec.id for rec in SeqIO.parse(handle, "fasta")]
handle.seek(0)
[len(rec) for rec in SeqIO.parse(handle, "fasta")]
As Devon suggests, I doubt using seek rather just re-opening the handle each time will make much difference performance-wise.
• 0 views
•
link
Log in to answer this question.
Is the performance hit of closing and reopening the file that bad?