Imported Upstream version 0.5
[pysam.git] / pysam / TabProxies.pyx
index 6e3a866d07c212b148cb7c4835a862ba85842ced..36295ebe12ccba9584b2b30be907fe848601f537 100644 (file)
@@ -141,25 +141,16 @@ cdef class TupleProxy:
 
         self.nfields = field
 
-    def _getindex( self, int index ):
-        '''return item at idx index'''
-        cdef int i = index
+    def __getitem__( self, key ):
+
+        cdef int i = key
         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 %i >= %i" % (i, self.nfields ))
+            raise IndexError( "list index out of range)
         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
@@ -288,8 +279,7 @@ 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
@@ -453,8 +443,8 @@ cdef class GTFProxy( TupleProxy ):
         r = self.attributes
         return [ x.strip().split(" ")[0] for x in r.split(";") if x.strip() != '' ]
 
-    def __getitem__(self, key):
-        return self.__getattr__( key )
+    def __getitem__(self, item):
+        return self.__getattr__( item )
 
     def __getattr__(self, item ):
         """Generic lookup of attribute from GFF/GTF attributes 
@@ -473,13 +463,18 @@ 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
-            result = PyString_FromStringAndSize( start, l )
+            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)
             return result
         else:
             return start
@@ -606,15 +601,10 @@ 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": 
@@ -624,4 +614,4 @@ cdef class VCFProxy( NamedTupleProxy ):
         cdef int idx
         idx, f = self.map_key2field[key]
         TupleProxy._setindex(self, idx, str(value) )
-
+