Imported Upstream version 0.6
[pysam.git] / pysam / TabProxies.pyx
index 36295ebe12ccba9584b2b30be907fe848601f537..6e3a866d07c212b148cb7c4835a862ba85842ced 100644 (file)
@@ -141,16 +141,25 @@ cdef class TupleProxy:
 
         self.nfields = field
 
-    def __getitem__( self, key ):
-
-        cdef int i = key
+    def _getindex( self, int index ):
+        '''return item at idx index'''
+        cdef int i = index
         if i < 0: i += self.nfields
         if i < 0: raise IndexError( "list index out of range" )
         i += self.offset
         if i >= self.nfields:
-            raise IndexError( "list index out of range)
+            raise IndexError( "list index out of range %i >= %i" % (i, self.nfields ))
         return self.fields[i]
 
+    def __getitem__( self, key ):
+        if type(key) == int: return self._getindex( key )
+        # slice object
+        start, end, step = key.indices( self.nfields )
+        result = []
+        for index in range( start, end, step ):
+            result.append( self._getindex( index ) )
+        return result
+
     def _setindex( self, index, value ):
         '''set item at idx index.'''
         cdef int idx = index
@@ -279,7 +288,8 @@ cdef class GTFProxy( TupleProxy ):
 
         self.start = atoi( cstart ) - 1
         self.end = atoi( cend )
-                      
+        self.nfields = 9
+       
     property contig:
        '''contig of feature.'''
        def __get__( self ): return self.contig
@@ -443,8 +453,8 @@ cdef class GTFProxy( TupleProxy ):
         r = self.attributes
         return [ x.strip().split(" ")[0] for x in r.split(";") if x.strip() != '' ]
 
-    def __getitem__(self, item):
-        return self.__getattr__( item )
+    def __getitem__(self, key):
+        return self.__getattr__( key )
 
     def __getattr__(self, item ):
         """Generic lookup of attribute from GFF/GTF attributes 
@@ -463,18 +473,13 @@ cdef class GTFProxy( TupleProxy ):
 
         start += strlen(query) + 1
         # skip gaps before
-        while start[0] == " ": start += 1
+        while start[0] == ' ': start += 1
         if start[0] == '"':
             start += 1
             end = start
             while end[0] != '\0' and end[0] != '"': end += 1
-            l = end - start + 1
-            cpy = <char*>calloc( l, sizeof(char ) )
-            if cpy == NULL: raise ValueError("out of memory" )
-            memcpy( cpy, start, l )
-            cpy[l-1] = '\0'
-            result = cpy
-            free(cpy)
+            l = end - start
+            result = PyString_FromStringAndSize( start, l )
             return result
         else:
             return start
@@ -601,10 +606,15 @@ cdef class VCFProxy( NamedTupleProxy ):
         self.contig = self.fields[0]
         # vcf counts from 1 - correct here
         self.pos = atoi( self.fields[1] ) - 1
-
+                             
     def __len__(self):
         return max(0, self.nfields - 9)
 
+    property pos:
+       '''feature end (in 0-based open/closed coordinates).'''
+       def __get__( self ): 
+           return self.pos
+
     def __setattr__(self, key, value ):
         '''set attribute.'''
         if key == "pos": 
@@ -614,4 +624,4 @@ cdef class VCFProxy( NamedTupleProxy ):
         cdef int idx
         idx, f = self.map_key2field[key]
         TupleProxy._setindex(self, idx, str(value) )
-    
+