[Biopython] writing sequences one by one using SeqIO

Peter Cock p.j.a.cock at googlemail.com
Sat Jun 8 09:53:52 UTC 2013


On Friday, June 7, 2013, Iddo Friedberg wrote:

> Hi,
>
> Probably has been asked & answered on this list, but I cannot find it.
>
> My problem is doing a SeqIO read and SeqIO.write of sequences one by one
> instead of in bulk to save memory. The wiki  as an example of generating an
> iterator in a single shot. .
>
> http://biopython.org/wiki/SeqIO#Input.2FOutput_Example_-_Filtering_by_sequence_length
>
> However, my processing of sequences is a bit more involved than that, and
> requires several lines of code. Do I have to resorlt to calling an iterator
> function, or is there a way to read a single sequence, process, & then
> write it before the next iteration?
>
> Thanks,
>
> Iddo
>

Hi Iddo,

For simple sequential files (without any headers etc to worry about),
open the handle, call SeqIO.write multiple times, close the handle.

Although I would agree with Lenna that if you can refactor the
code to use an iterator approach, it is often more pleasing.

Peter



More information about the Biopython mailing list