[Biopython-dev] [Bug 2348] New: Slicing the Seq object (returns a string when use a stride)

bugzilla-daemon at portal.open-bio.org bugzilla-daemon at portal.open-bio.org
Thu Aug 16 21:15:11 UTC 2007


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

           Summary: Slicing the Seq object (returns a string when use a
                    stride)
           Product: Biopython
           Version: 1.43
          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


I think this is a bug introduced to changes in how python deals with splicing. 
Currently we have the following in Bio/Seq/Seq.py:

class Seq:
    ...
    def __getitem__(self, i): return self.data[i]  # Seq API requirement
    def __getslice__(self, i, j):                  # Seq API requirement
        i = max(i, 0); j = max(j, 0)
        return Seq(self.data[i:j], self.alphabet)

Quoting: http://docs.python.org/ref/sequence-methods.html
> __getslice__
> Deprecated since release 2.0. Support slice objects as parameters
> to the > __getitem__() method.

Here is an example of how the current code can fail on any Python 2.x version.
These all work:

from Bio.Seq import Seq
x = Seq('ACTATCGTAGTACGGCT')
assert isinstance(x[0], str)
assert isinstance(x[1], str)
assert isinstance(x[2], str)
assert isinstance(x[-1], str)
assert isinstance(x[1:5], Seq)
assert isinstance(x[0:-1], Seq)
assert isinstance(x[:], Seq)

But, the following variants using a stride will give a string because they are
handled by our old fashioned __getitem__ method:

x[1:2:3]
x[slice(1, 2)]
x[slice(1, 2, 3)]
x[slice(None)]
x[slice(None, None)]
x[slice(None, None, None)]
x[::-1]
x[slice(None, None, -1)]

The last two return a reversed string (rather than a reversed Seq)

I propose we remove the Seq object's __getslice__ method, and replace the
__getitem__ method with a slice aware version. I'll prepare a patch...


-- 
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