[Biopython-dev] [Bug 2561] New: SeqRecord format method to get a string in a given file format

bugzilla-daemon at portal.open-bio.org bugzilla-daemon at portal.open-bio.org
Fri Aug 1 05:41:14 EDT 2008


http://bugzilla.open-bio.org/show_bug.cgi?id=2561

           Summary: SeqRecord format method to get a string in a given file
                    format
           Product: Biopython
           Version: Not Applicable
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Main Distribution
        AssignedTo: biopython-dev at biopython.org
        ReportedBy: biopython-bugzilla at maubp.freeserve.co.uk


If you have a SeqRecord, it is sometimes useful to be be able to convert it
into a FASTA format string, or indeed any suitable file format.  Note that this
only makes sense for file formats which support a single record, such as
sequential formats like FASTA, GenBank, EMBL, SwissProt, ...

See http://portal.open-bio.org/pipermail/biopython-dev/2008-June/003793.html

PEP 3101 "Advanced String Formatting" describes a new __format__ method for
objects wishing to support the new python format() function in Python 2.6 and
3.0, see http://www.python.org/dev/peps/pep-3101/

In the short term we could expose this functionality as a method named 
tostring(), to_string(), to_format() or some other suitable suggestion.  Using
tostring() would be consistent with the Bio.Seq.Seq and Bio.Seq.MutableSeq
objects (although those do not take a format argument).

This could be implemented using Bio.SeqIO with a StringIO handle, for example:

######################################
For the SeqRecord class, in Bio/SeqRecord.py
######################################
    def tostring(self, format=None) :
        """Returns the record as a string in the specified file format.

        If the file format is omitted (default), the sequence itself is
        returned as a string.

        Otherwise the format should be a lower case string supported by
        Bio.SeqIO, which is used to turn the SeqRecord into a string."""
        if format :
            from StringIO import StringIO
            from Bio import SeqIO
            handle = StringIO()
            SeqIO.write([self], handle, format)
            handle.seek(0)
            return handle.read()
        else :
            #Return the sequence as a string
            return self.seq.tostring()
############################################


-- 
Configure bugmail: http://bugzilla.open-bio.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.


More information about the Biopython-dev mailing list