[Biopython] writing sequences one by one using SeqIO

Lenna Peterson arklenna at gmail.com
Fri Jun 7 19:48:22 UTC 2013


In my opinion, iterator functions are one of Python's most powerful tools.
In the linked example, you could replace the generator comprehension with
the following function::

def short_seq_gen():
    for record in input_seq_iterator:
        if len(record.seq) < 300:
            yield record

output_handle = open("short_seqs.fasta", "w")
SeqIO.write(short_seq_gen(), output_handle, "fasta")  # note parentheses to
call function


I don't think there's a better way to do this.

Cheers,

Lenna



On Fri, Jun 7, 2013 at 3:15 PM, Iddo Friedberg <idoerg at gmail.com> 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
>
> --
> Iddo Friedberg
> http://iddo-friedberg.net/contact.html
> ++++++++++[>+++>++++++>++++++++>++++++++++>+++++++++++<<<<<-]>>>>++++.>
> ++++++..----.<<<<++++++++++++++++++++++++++++.-----------..>>>+.-----.
> .>-.<<<<--.>>>++.>+++.<+++.----.-.<++++++++++++++++++.>+.>.<++.<<<+.>>
> >>----.<--.>++++++.<<<<------------------------------------.
> _______________________________________________
> Biopython mailing list  -  Biopython at lists.open-bio.org
> http://lists.open-bio.org/mailman/listinfo/biopython
>



More information about the Biopython mailing list