[BioPython] SeqRecord to Genbank: use SeqIO?

Peter biopython at maubp.freeserve.co.uk
Fri Aug 1 10:21:06 UTC 2008


On Fri, Aug 1, 2008 at 10:20 AM, Peter Cock <p.j.a.cock at googlemail.com> wrote:
> On Fri, Aug 1, 2008 at 2:45 AM, Cedar McKay <cmckay at u.washington.edu> wrote:
>> Hello, I have SeqRecord objects that I'd like to convert to a string ...
>> And it [Bio.SeqIO.write()] has to write to a file handle for some reason,
>> and won't just give me the string to do whatever I want with.
>
> This is by design - the API uses handles and only handles.  If you
> want a string containing the data, use StringIO (or cStringIO),
> something like this:
>
> from StringIO import StringIO
> handle = StringIO()
> SeqIO.write(seq_records, handle "fasta")
> handle.seek(0)
> data = handle.read()
>
> This isn't in the tutorial or the wiki page (yet).
> http://biopython.org/wiki/SeqIO

I've just updated the tutorial in CVS to cover this (and a similar
example for Bio.AlignIO).  We don't normally update the public PDF and
HTML versions of the tutorial between releases to avoid the
documentation talking about unreleased features, so you won't see this
change for a while.

Could I ask why you want to get the SeqRecord as a string in GenBank
format?  My only guess is as part of some webservice where a string
would be useful to embed within a page template.  Getting a SeqRecord
in FASTA format is probably a more common request, given many web
tools will take this as input.

Peter



More information about the Biopython mailing list