[Biopython] SeqIO.write and user-specified wrapping

Peter Cock p.j.a.cock at googlemail.com
Mon Mar 4 16:54:11 UTC 2013


On Mon, Mar 4, 2013 at 4:14 PM, Ivan Gregoretti <ivangreg at gmail.com> wrote:
> Hi Everybody,
>
> Could a kind soul show how to produce a FASTA file without wrapping
> its sequences?
>
> I am looking for something like this:
>
> from Bio import SeqIO
> SeqIO.write(my_records, "my_example.fa", "fasta", wrap=0)
>
> as opposed to the default wrapping after 60 characters.
>
> Thanks for Biopython.
>
> Ivan

Currently SeqIO doesn't allow extra file format specific
argument like that (although it could in principle). Here
you must currently use the underlying writer directly,
something like this:

from Bio.SeqIO.FastaIO import FastaWriter
handle = open("my_example.fa", "w")
writer = FastaWriter(handle, wrap=0)
writer.write_file(my_records)
handle.close()

Or, since you want no line wrapping you could do
something like this instead:

handle = open("my_example.fa", "w")
for record in my_records:
    handle.write(">%s %s\n%s\n" % (record.id, record.description, record.seq))
handle.close()

Peter



More information about the Biopython mailing list