[BioPython] question regarding writing Seq objects in Fasta format

Peter biopython at maubp.freeserve.co.uk
Tue Mar 27 11:35:54 UTC 2007


On 3/27/07, Ann Loraine <aloraine at gmail.com> wrote:
> Hello,
>
> I have a question about how to write out Bio.Seq.Seq objects to a
> fasta format file.
>
> I've generated a lot of these by translating segments of genomic
> sequence -- see below.
>
> What objects or code should I use?

I would suggest you try the new Bio.SeqIO code described here, you
will need BioPython 1.43 or later:

http://biopython.org/wiki/SeqIO

You'll need to "upgrade" your Seq objects into SeqRecord objects and
give them an identifier before calling Bio.SeqIO.write() on them.

We'd welcome feedback on this, for example if there are any errors in
the newly writenupdated documentation.

> It looks like Bio.SeqIO.FASTA.FastaWriter might be the right thing,
> but it doesn't appear to accept Bio.Seq.Seq objects. Do I need to
> create a new type of Seq-like object before I can use
> Bio.SeqIO.FASTA.FastaWriter?

We plan to mark that particular (undocumented) bit of BioPython as
depriated in the next release - so I can't really recommend using it.

Personally, before working on Bio.SeqIO I used to write fasta files
"by hand" using something like this:

handle = open("example.faa", "w")
for identifier, seq in some_list_of_tuples :
    handle.write(">%s\n%s\n" % (identifier, seq.tostring()))
handle.close()

where identifier is a string, and seq is a BioPython Seq object.
There is a lot to be said for doing it "by hand" if you want full
control over the description for example.

Peter



More information about the Biopython mailing list