Commits

James Taylor committed 39d269c

Add a "do_mask" flag for 2bit files. This is by far the slowest part of
working with these files, so if you aren't using the soft masking you can
turn it off. However 'N' masking is always done, since the data is invalid
without it.

Comments (0)

Files changed (3)

lib/bx/seq/_twobit.c

-/* Generated by Pyrex 0.9.5.1a on Sun Aug 12 11:47:58 2007 */
+/* Generated by Cython 0.9.6.14 on Tue May 13 14:02:43 2008 */
 
+#define PY_SSIZE_T_CLEAN
 #include "Python.h"
 #include "structmember.h"
 #ifndef PY_LONG_LONG
   #define PY_LONG_LONG LONG_LONG
 #endif
+#if PY_VERSION_HEX < 0x02050000
+  typedef int Py_ssize_t;
+  #define PY_SSIZE_T_MAX INT_MAX
+  #define PY_SSIZE_T_MIN INT_MIN
+  #define PyInt_FromSsize_t(z) PyInt_FromLong(z)
+  #define PyInt_AsSsize_t(o)   PyInt_AsLong(o)
+  #define PyNumber_Index(o)    PyNumber_Int(o)
+  #define PyIndex_Check(o)     PyNumber_Check(o)
+#endif
+#if PY_VERSION_HEX < 0x02040000
+  #define METH_COEXIST 0
+#endif
+#ifndef __stdcall
+  #define __stdcall
+#endif
+#ifndef __cdecl
+  #define __cdecl
+#endif
 #ifdef __cplusplus
 #define __PYX_EXTERN_C extern "C"
 #else
 #define __PYX_EXTERN_C extern
 #endif
-__PYX_EXTERN_C double pow(double, double);
+#include <math.h>
 #include "ctype.h"
 #include "string.h"
 
 
+#ifdef __GNUC__
+#define INLINE __inline__
+#elif _WIN32
+#define INLINE __inline
+#else
+#define INLINE 
+#endif
+
 typedef struct {PyObject **p; char *s;} __Pyx_InternTabEntry; /*proto*/
-typedef struct {PyObject **p; char *s; long n;} __Pyx_StringTabEntry; /*proto*/
+typedef struct {PyObject **p; char *s; long n; int is_unicode;} __Pyx_StringTabEntry; /*proto*/
 
+
+
+static int __pyx_skip_dispatch = 0;
+
+
+/* Type Conversion Predeclarations */
+
+#define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False))
+static INLINE int __Pyx_PyObject_IsTrue(PyObject* x);
+static INLINE PY_LONG_LONG __pyx_PyInt_AsLongLong(PyObject* x);
+static INLINE unsigned PY_LONG_LONG __pyx_PyInt_AsUnsignedLongLong(PyObject* x);
+static INLINE Py_ssize_t __pyx_PyIndex_AsSsize_t(PyObject* b);
+
+#define __pyx_PyInt_AsLong(x) (PyInt_CheckExact(x) ? PyInt_AS_LONG(x) : PyInt_AsLong(x))
+#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
+
+static INLINE unsigned char __pyx_PyInt_unsigned_char(PyObject* x);
+static INLINE unsigned short __pyx_PyInt_unsigned_short(PyObject* x);
+static INLINE char __pyx_PyInt_char(PyObject* x);
+static INLINE short __pyx_PyInt_short(PyObject* x);
+static INLINE int __pyx_PyInt_int(PyObject* x);
+static INLINE long __pyx_PyInt_long(PyObject* x);
+static INLINE signed char __pyx_PyInt_signed_char(PyObject* x);
+static INLINE signed short __pyx_PyInt_signed_short(PyObject* x);
+static INLINE signed int __pyx_PyInt_signed_int(PyObject* x);
+static INLINE signed long __pyx_PyInt_signed_long(PyObject* x);
+static INLINE long double __pyx_PyInt_long_double(PyObject* x);
+#ifdef __GNUC__
+/* Test for GCC > 2.95 */
+#if __GNUC__ > 2 ||               (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)) 
+#define likely(x)   __builtin_expect(!!(x), 1)
+#define unlikely(x) __builtin_expect(!!(x), 0)
+#else /* __GNUC__ > 2 ... */
+#define likely(x)   (x)
+#define unlikely(x) (x)
+#endif /* __GNUC__ > 2 ... */
+#else /* __GNUC__ */
+#define likely(x)   (x)
+#define unlikely(x) (x)
+#endif /* __GNUC__ */
+    
 static PyObject *__pyx_m;
 static PyObject *__pyx_b;
+static PyObject *__pyx_empty_tuple;
 static int __pyx_lineno;
+static int __pyx_clineno = 0;
+static char * __pyx_cfilenm= __FILE__;
 static char *__pyx_filename;
 static char **__pyx_f;
 
+static INLINE void __Pyx_RaiseArgtupleTooLong(Py_ssize_t num_expected, Py_ssize_t num_found); /*proto*/
+
 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/
 
 static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/
 
 static void __Pyx_AddTraceback(char *funcname); /*proto*/
 
-/* Declarations from _twobit */
+/* Declarations */
 
-static char (*__pyx_v_7_twobit_valToNt);
-static PyObject *__pyx_k6;
+static char *__pyx_v_2bx_3seq_7_twobit_valToNt;
 
 
-/* Implementation of _twobit */
+/* Implementation of bx.seq._twobit */
 
-static char (__pyx_k5[]) = "TCAG";
+static char __pyx_k_5[] = "TCAG";
 
 static PyObject *__pyx_n_struct;
 static PyObject *__pyx_n_sys;
 static PyObject *__pyx_n_bisect;
-static PyObject *__pyx_n_read;
-static PyObject *__pyx_n_False;
+
+/* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":19
+ * valToNt = "TCAG"
+ * 
+ * def read( file, seq, int fragStart, int fragEnd, do_mask=False ):             # <<<<<<<<<<<<<< 
+ *     """
+ *     Stolen directly from Jim Kent's twoBit.c
+ */
+
+static PyObject *__pyx_int_0;
+static PyObject *__pyx_int_1;
 
 static PyObject *__pyx_n_seek;
 static PyObject *__pyx_n_sequence_offset;
-static PyObject *__pyx_n_len;
+static PyObject *__pyx_n_read;
 static PyObject *__pyx_n_n_block_starts;
 static PyObject *__pyx_n_n_block_sizes;
 static PyObject *__pyx_n_masked_block_starts;
 static PyObject *__pyx_n_masked_block_sizes;
 
-static char (__pyx_k7[]) = "N";
+static char __pyx_k_6[] = "N";
 
-static PyObject *__pyx_f_7_twobit_read(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
-static char __pyx_doc_7_twobit_read[] = "\n    Stolen directly from Jim Kent\'s twoBit.c\n    ";
-static PyObject *__pyx_f_7_twobit_read(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+static PyObject *__pyx_pf_2bx_3seq_7_twobit_read(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_2bx_3seq_7_twobit_read[] = "\n    Stolen directly from Jim Kent\'s twoBit.c\n    ";
+static PyObject *__pyx_pf_2bx_3seq_7_twobit_read(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
   PyObject *__pyx_v_file = 0;
   PyObject *__pyx_v_seq = 0;
   int __pyx_v_fragStart;
   int __pyx_v_j;
   int __pyx_v_s;
   int __pyx_v_e;
-  char (*__pyx_v_packed);
-  char (*__pyx_v_dna);
+  char *__pyx_v_packed;
+  char *__pyx_v_dna;
   char __pyx_v_partial;
   PyObject *__pyx_v_dna_py;
   PyObject *__pyx_v_packed_py;
   PyObject *__pyx_3 = 0;
   PyObject *__pyx_4 = 0;
   int __pyx_5;
-  long __pyx_6;
-  long __pyx_7;
+  Py_ssize_t __pyx_6 = 0;
+  int __pyx_7;
+  int __pyx_8;
+  int __pyx_9;
   static char *__pyx_argnames[] = {"file","seq","fragStart","fragEnd","do_mask",0};
-  __pyx_v_do_mask = __pyx_k6;
-  if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "OOii|O", __pyx_argnames, &__pyx_v_file, &__pyx_v_seq, &__pyx_v_fragStart, &__pyx_v_fragEnd, &__pyx_v_do_mask)) return 0;
-  Py_INCREF(__pyx_v_file);
-  Py_INCREF(__pyx_v_seq);
-  Py_INCREF(__pyx_v_do_mask);
+  __pyx_v_do_mask = Py_False;
+  if (likely(!__pyx_kwds) && likely(4 <= PyTuple_GET_SIZE(__pyx_args)) && likely(PyTuple_GET_SIZE(__pyx_args) <= 5)) {
+    __pyx_v_file = PyTuple_GET_ITEM(__pyx_args, 0);
+    __pyx_v_seq = PyTuple_GET_ITEM(__pyx_args, 1);
+    __pyx_v_fragStart = __pyx_PyInt_int(PyTuple_GET_ITEM(__pyx_args, 2)); if (unlikely((__pyx_v_fragStart == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L2;}
+    __pyx_v_fragEnd = __pyx_PyInt_int(PyTuple_GET_ITEM(__pyx_args, 3)); if (unlikely((__pyx_v_fragEnd == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L2;}
+    if (PyTuple_GET_SIZE(__pyx_args) > 4) {
+      __pyx_v_do_mask = PyTuple_GET_ITEM(__pyx_args, 4);
+    }
+  }
+  else {
+    if (unlikely(!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "OOii|O", __pyx_argnames, &__pyx_v_file, &__pyx_v_seq, &__pyx_v_fragStart, &__pyx_v_fragEnd, &__pyx_v_do_mask))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L2;}
+  }
+  goto __pyx_L3;
+  __pyx_L2:;
+  __Pyx_AddTraceback("bx.seq._twobit.read");
+  return NULL;
+  __pyx_L3:;
   __pyx_v_dna_py = Py_None; Py_INCREF(Py_None);
   __pyx_v_packed_py = Py_None; Py_INCREF(Py_None);
   __pyx_v_midEnd = Py_None; Py_INCREF(Py_None);
   __pyx_v_start_ix = Py_None; Py_INCREF(Py_None);
   __pyx_v_m_block_count = Py_None; Py_INCREF(Py_None);
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":29 */
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":29
+ *     cdef char * packed, * dna, *dna_orig
+ *     cdef char partial
+ *     packedStart = (fragStart>>2);             # <<<<<<<<<<<<<< 
+ *     packedEnd = ((fragEnd+3)>>2);
+ *     packByteCount = packedEnd - packedStart;
+ */
   __pyx_v_packedStart = (__pyx_v_fragStart >> 2);
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":30 */
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":30
+ *     cdef char partial
+ *     packedStart = (fragStart>>2);
+ *     packedEnd = ((fragEnd+3)>>2);             # <<<<<<<<<<<<<< 
+ *     packByteCount = packedEnd - packedStart;
+ *     # Empty string in which to write unpacked DNA
+ */
   __pyx_v_packedEnd = ((__pyx_v_fragEnd + 3) >> 2);
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":31 */
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":31
+ *     packedStart = (fragStart>>2);
+ *     packedEnd = ((fragEnd+3)>>2);
+ *     packByteCount = packedEnd - packedStart;             # <<<<<<<<<<<<<< 
+ *     # Empty string in which to write unpacked DNA
+ *     dna_py = PyString_FromStringAndSize( NULL, fragEnd - fragStart )
+ */
   __pyx_v_packByteCount = (__pyx_v_packedEnd - __pyx_v_packedStart);
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":33 */
-  __pyx_1 = PyString_FromStringAndSize(NULL,(__pyx_v_fragEnd - __pyx_v_fragStart)); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;}
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":33
+ *     packByteCount = packedEnd - packedStart;
+ *     # Empty string in which to write unpacked DNA
+ *     dna_py = PyString_FromStringAndSize( NULL, fragEnd - fragStart )             # <<<<<<<<<<<<<< 
+ *     dna = PyString_AsString( dna_py )
+ *     # Read it
+ */
+  __pyx_1 = PyString_FromStringAndSize(NULL, (__pyx_v_fragEnd - __pyx_v_fragStart)); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L1;}
   Py_DECREF(__pyx_v_dna_py);
   __pyx_v_dna_py = __pyx_1;
   __pyx_1 = 0;
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":34 */
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":34
+ *     # Empty string in which to write unpacked DNA
+ *     dna_py = PyString_FromStringAndSize( NULL, fragEnd - fragStart )
+ *     dna = PyString_AsString( dna_py )             # <<<<<<<<<<<<<< 
+ *     # Read it
+ *     file.seek( seq.sequence_offset + packedStart )
+ */
   __pyx_v_dna = PyString_AsString(__pyx_v_dna_py);
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":36 */
-  __pyx_1 = PyObject_GetAttr(__pyx_v_file, __pyx_n_seek); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; goto __pyx_L1;}
-  __pyx_2 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_sequence_offset); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; goto __pyx_L1;}
-  __pyx_3 = PyInt_FromLong(__pyx_v_packedStart); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; goto __pyx_L1;}
-  __pyx_4 = PyNumber_Add(__pyx_2, __pyx_3); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; goto __pyx_L1;}
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":36
+ *     dna = PyString_AsString( dna_py )
+ *     # Read it
+ *     file.seek( seq.sequence_offset + packedStart )             # <<<<<<<<<<<<<< 
+ *     packed_py = file.read( packByteCount )
+ *     packed = PyString_AsString( packed_py )
+ */
+  __pyx_1 = PyObject_GetAttr(__pyx_v_file, __pyx_n_seek); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  __pyx_2 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_sequence_offset); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  __pyx_3 = PyInt_FromLong(__pyx_v_packedStart); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  __pyx_4 = PyNumber_Add(__pyx_2, __pyx_3); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1;}
   Py_DECREF(__pyx_2); __pyx_2 = 0;
   Py_DECREF(__pyx_3); __pyx_3 = 0;
-  __pyx_2 = PyTuple_New(1); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; goto __pyx_L1;}
+  __pyx_2 = PyTuple_New(1); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1;}
   PyTuple_SET_ITEM(__pyx_2, 0, __pyx_4);
   __pyx_4 = 0;
-  __pyx_3 = PyObject_CallObject(__pyx_1, __pyx_2); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; goto __pyx_L1;}
+  __pyx_3 = PyObject_Call(__pyx_1, ((PyObject *)__pyx_2), NULL); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1;}
   Py_DECREF(__pyx_1); __pyx_1 = 0;
-  Py_DECREF(__pyx_2); __pyx_2 = 0;
+  Py_DECREF(((PyObject *)__pyx_2)); __pyx_2 = 0;
   Py_DECREF(__pyx_3); __pyx_3 = 0;
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":37 */
-  __pyx_4 = PyObject_GetAttr(__pyx_v_file, __pyx_n_read); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 37; goto __pyx_L1;}
-  __pyx_1 = PyInt_FromLong(__pyx_v_packByteCount); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 37; goto __pyx_L1;}
-  __pyx_2 = PyTuple_New(1); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 37; goto __pyx_L1;}
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":37
+ *     # Read it
+ *     file.seek( seq.sequence_offset + packedStart )
+ *     packed_py = file.read( packByteCount )             # <<<<<<<<<<<<<< 
+ *     packed = PyString_AsString( packed_py )
+ *     # Handle case where everything is in one packed byte 
+ */
+  __pyx_4 = PyObject_GetAttr(__pyx_v_file, __pyx_n_read); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  __pyx_1 = PyInt_FromLong(__pyx_v_packByteCount); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  __pyx_2 = PyTuple_New(1); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1;}
   PyTuple_SET_ITEM(__pyx_2, 0, __pyx_1);
   __pyx_1 = 0;
-  __pyx_3 = PyObject_CallObject(__pyx_4, __pyx_2); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 37; goto __pyx_L1;}
+  __pyx_3 = PyObject_Call(__pyx_4, ((PyObject *)__pyx_2), NULL); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1;}
   Py_DECREF(__pyx_4); __pyx_4 = 0;
-  Py_DECREF(__pyx_2); __pyx_2 = 0;
+  Py_DECREF(((PyObject *)__pyx_2)); __pyx_2 = 0;
   Py_DECREF(__pyx_v_packed_py);
   __pyx_v_packed_py = __pyx_3;
   __pyx_3 = 0;
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":38 */
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":38
+ *     file.seek( seq.sequence_offset + packedStart )
+ *     packed_py = file.read( packByteCount )
+ *     packed = PyString_AsString( packed_py )             # <<<<<<<<<<<<<< 
+ *     # Handle case where everything is in one packed byte 
+ *     if packByteCount == 1:
+ */
   __pyx_v_packed = PyString_AsString(__pyx_v_packed_py);
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":40 */
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":40
+ *     packed = PyString_AsString( packed_py )
+ *     # Handle case where everything is in one packed byte 
+ *     if packByteCount == 1:             # <<<<<<<<<<<<<< 
+ *         pOff = (packedStart<<2)
+ *         pStart = fragStart - pOff
+ */
   __pyx_5 = (__pyx_v_packByteCount == 1);
   if (__pyx_5) {
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":41 */
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":41
+ *     # Handle case where everything is in one packed byte 
+ *     if packByteCount == 1:
+ *         pOff = (packedStart<<2)             # <<<<<<<<<<<<<< 
+ *         pStart = fragStart - pOff
+ *         pEnd = fragEnd - pOff
+ */
     __pyx_v_pOff = (__pyx_v_packedStart << 2);
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":42 */
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":42
+ *     if packByteCount == 1:
+ *         pOff = (packedStart<<2)
+ *         pStart = fragStart - pOff             # <<<<<<<<<<<<<< 
+ *         pEnd = fragEnd - pOff
+ *         partial = packed[0]
+ */
     __pyx_v_pStart = (__pyx_v_fragStart - __pyx_v_pOff);
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":43 */
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":43
+ *         pOff = (packedStart<<2)
+ *         pStart = fragStart - pOff
+ *         pEnd = fragEnd - pOff             # <<<<<<<<<<<<<< 
+ *         partial = packed[0]
+ *         assert pEnd <= 4
+ */
     __pyx_v_pEnd = (__pyx_v_fragEnd - __pyx_v_pOff);
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":44 */
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":44
+ *         pStart = fragStart - pOff
+ *         pEnd = fragEnd - pOff
+ *         partial = packed[0]             # <<<<<<<<<<<<<< 
+ *         assert pEnd <= 4
+ *         assert pStart >= 0
+ */
     __pyx_v_partial = (__pyx_v_packed[0]);
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":45 */
-    if (!(__pyx_v_pEnd <= 4)) {
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":45
+ *         pEnd = fragEnd - pOff
+ *         partial = packed[0]
+ *         assert pEnd <= 4             # <<<<<<<<<<<<<< 
+ *         assert pStart >= 0
+ *         for i from pStart <= i < pEnd:
+ */
+    #ifndef PYREX_WITHOUT_ASSERTIONS
+    if (unlikely(!(__pyx_v_pEnd <= 4))) {
       PyErr_SetNone(PyExc_AssertionError);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 45; goto __pyx_L1;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 45; __pyx_clineno = __LINE__; goto __pyx_L1;}
     }
+    #endif
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":46 */
-    if (!(__pyx_v_pStart >= 0)) {
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":46
+ *         partial = packed[0]
+ *         assert pEnd <= 4
+ *         assert pStart >= 0             # <<<<<<<<<<<<<< 
+ *         for i from pStart <= i < pEnd:
+ *             dna[0] = valToNt[(partial >> (6-i-i)) & 3]
+ */
+    #ifndef PYREX_WITHOUT_ASSERTIONS
+    if (unlikely(!(__pyx_v_pStart >= 0))) {
       PyErr_SetNone(PyExc_AssertionError);
-      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; goto __pyx_L1;}
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1;}
     }
+    #endif
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":47 */
-    for (__pyx_v_i = __pyx_v_pStart; __pyx_v_i < __pyx_v_pEnd; ++__pyx_v_i) {
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":47
+ *         assert pEnd <= 4
+ *         assert pStart >= 0
+ *         for i from pStart <= i < pEnd:             # <<<<<<<<<<<<<< 
+ *             dna[0] = valToNt[(partial >> (6-i-i)) & 3]
+ *             dna = dna + 1
+ */
+    for (__pyx_v_i = __pyx_v_pStart; __pyx_v_i < __pyx_v_pEnd; __pyx_v_i++) {
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":48 */
-      (__pyx_v_dna[0]) = (__pyx_v_7_twobit_valToNt[((__pyx_v_partial >> ((6 - __pyx_v_i) - __pyx_v_i)) & 3)]);
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":48
+ *         assert pStart >= 0
+ *         for i from pStart <= i < pEnd:
+ *             dna[0] = valToNt[(partial >> (6-i-i)) & 3]             # <<<<<<<<<<<<<< 
+ *             dna = dna + 1
+ *     else:
+ */
+      (__pyx_v_dna[0]) = (__pyx_v_2bx_3seq_7_twobit_valToNt[((__pyx_v_partial >> ((6 - __pyx_v_i) - __pyx_v_i)) & 3)]);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":49 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":49
+ *         for i from pStart <= i < pEnd:
+ *             dna[0] = valToNt[(partial >> (6-i-i)) & 3]
+ *             dna = dna + 1             # <<<<<<<<<<<<<< 
+ *     else:
+ *         # Handle partial first packed byte.
+ */
       __pyx_v_dna = (__pyx_v_dna + 1);
     }
-    goto __pyx_L2;
+    goto __pyx_L4;
   }
   /*else*/ {
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":52 */
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":52
+ *     else:
+ *         # Handle partial first packed byte.
+ *         midStart = fragStart;             # <<<<<<<<<<<<<< 
+ *         remainder = ( fragStart&3 )
+ *         if remainder > 0:
+ */
     __pyx_v_midStart = __pyx_v_fragStart;
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":53 */
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":53
+ *         # Handle partial first packed byte.
+ *         midStart = fragStart;
+ *         remainder = ( fragStart&3 )             # <<<<<<<<<<<<<< 
+ *         if remainder > 0:
+ *             partial = packed[0]; packed = packed + 1
+ */
     __pyx_v_remainder = (__pyx_v_fragStart & 3);
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":54 */
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":54
+ *         midStart = fragStart;
+ *         remainder = ( fragStart&3 )
+ *         if remainder > 0:             # <<<<<<<<<<<<<< 
+ *             partial = packed[0]; packed = packed + 1
+ *             partCount = 4 - remainder;
+ */
     __pyx_5 = (__pyx_v_remainder > 0);
     if (__pyx_5) {
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":55 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":55
+ *         remainder = ( fragStart&3 )
+ *         if remainder > 0:
+ *             partial = packed[0]; packed = packed + 1             # <<<<<<<<<<<<<< 
+ *             partCount = 4 - remainder;
+ *             for i from partCount - 1 >= i >= 0:
+ */
       __pyx_v_partial = (__pyx_v_packed[0]);
-
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":55 */
       __pyx_v_packed = (__pyx_v_packed + 1);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":56 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":56
+ *         if remainder > 0:
+ *             partial = packed[0]; packed = packed + 1
+ *             partCount = 4 - remainder;             # <<<<<<<<<<<<<< 
+ *             for i from partCount - 1 >= i >= 0:
+ *                 dna[i] = valToNt[ partial & 3 ]
+ */
       __pyx_v_partCount = (4 - __pyx_v_remainder);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":57 */
-      for (__pyx_v_i = (__pyx_v_partCount - 1); __pyx_v_i >= 0; --__pyx_v_i) {
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":57
+ *             partial = packed[0]; packed = packed + 1
+ *             partCount = 4 - remainder;
+ *             for i from partCount - 1 >= i >= 0:             # <<<<<<<<<<<<<< 
+ *                 dna[i] = valToNt[ partial & 3 ]
+ *                 partial = partial >> 2
+ */
+      for (__pyx_v_i = (__pyx_v_partCount - 1); __pyx_v_i >= 0; __pyx_v_i--) {
 
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":58 */
-        (__pyx_v_dna[__pyx_v_i]) = (__pyx_v_7_twobit_valToNt[(__pyx_v_partial & 3)]);
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":58
+ *             partCount = 4 - remainder;
+ *             for i from partCount - 1 >= i >= 0:
+ *                 dna[i] = valToNt[ partial & 3 ]             # <<<<<<<<<<<<<< 
+ *                 partial = partial >> 2
+ *             midStart = midStart + partCount
+ */
+        (__pyx_v_dna[__pyx_v_i]) = (__pyx_v_2bx_3seq_7_twobit_valToNt[(__pyx_v_partial & 3)]);
 
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":59 */
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":59
+ *             for i from partCount - 1 >= i >= 0:
+ *                 dna[i] = valToNt[ partial & 3 ]
+ *                 partial = partial >> 2             # <<<<<<<<<<<<<< 
+ *             midStart = midStart + partCount
+ *             dna = dna + partCount
+ */
         __pyx_v_partial = (__pyx_v_partial >> 2);
       }
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":60 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":60
+ *                 dna[i] = valToNt[ partial & 3 ]
+ *                 partial = partial >> 2
+ *             midStart = midStart + partCount             # <<<<<<<<<<<<<< 
+ *             dna = dna + partCount
+ *         # Handle middle bytes.
+ */
       __pyx_v_midStart = (__pyx_v_midStart + __pyx_v_partCount);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":61 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":61
+ *                 partial = partial >> 2
+ *             midStart = midStart + partCount
+ *             dna = dna + partCount             # <<<<<<<<<<<<<< 
+ *         # Handle middle bytes.
+ *         remainder = fragEnd&3
+ */
       __pyx_v_dna = (__pyx_v_dna + __pyx_v_partCount);
-      goto __pyx_L5;
+      goto __pyx_L7;
     }
-    __pyx_L5:;
+    __pyx_L7:;
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":63 */
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":63
+ *             dna = dna + partCount
+ *         # Handle middle bytes.
+ *         remainder = fragEnd&3             # <<<<<<<<<<<<<< 
+ *         midEnd = fragEnd - remainder
+ *         i = midStart
+ */
     __pyx_v_remainder = (__pyx_v_fragEnd & 3);
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":64 */
-    __pyx_1 = PyInt_FromLong((__pyx_v_fragEnd - __pyx_v_remainder)); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; goto __pyx_L1;}
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":64
+ *         # Handle middle bytes.
+ *         remainder = fragEnd&3
+ *         midEnd = fragEnd - remainder             # <<<<<<<<<<<<<< 
+ *         i = midStart
+ *         while i < midEnd:
+ */
+    __pyx_1 = PyInt_FromLong((__pyx_v_fragEnd - __pyx_v_remainder)); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1;}
     Py_DECREF(__pyx_v_midEnd);
     __pyx_v_midEnd = __pyx_1;
     __pyx_1 = 0;
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":65 */
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":65
+ *         remainder = fragEnd&3
+ *         midEnd = fragEnd - remainder
+ *         i = midStart             # <<<<<<<<<<<<<< 
+ *         while i < midEnd:
+ *             partial = packed[0]
+ */
     __pyx_v_i = __pyx_v_midStart;
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":66 */
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":66
+ *         midEnd = fragEnd - remainder
+ *         i = midStart
+ *         while i < midEnd:             # <<<<<<<<<<<<<< 
+ *             partial = packed[0]
+ *             packed = packed + 1;
+ */
     while (1) {
-      __pyx_4 = PyInt_FromLong(__pyx_v_i); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; goto __pyx_L1;}
-      if (PyObject_Cmp(__pyx_4, __pyx_v_midEnd, &__pyx_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; goto __pyx_L1;}
-      __pyx_5 = __pyx_5 < 0;
+      __pyx_4 = PyInt_FromLong(__pyx_v_i); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      __pyx_2 = PyObject_RichCompare(__pyx_4, __pyx_v_midEnd, Py_LT); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1;}
       Py_DECREF(__pyx_4); __pyx_4 = 0;
+      __pyx_5 = __Pyx_PyObject_IsTrue(__pyx_2); if (unlikely(__pyx_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      Py_DECREF(__pyx_2); __pyx_2 = 0;
       if (!__pyx_5) break;
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":67 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":67
+ *         i = midStart
+ *         while i < midEnd:
+ *             partial = packed[0]             # <<<<<<<<<<<<<< 
+ *             packed = packed + 1;
+ *             dna[3] = valToNt[partial&3];
+ */
       __pyx_v_partial = (__pyx_v_packed[0]);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":68 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":68
+ *         while i < midEnd:
+ *             partial = packed[0]
+ *             packed = packed + 1;             # <<<<<<<<<<<<<< 
+ *             dna[3] = valToNt[partial&3];
+ *             partial = partial >> 2
+ */
       __pyx_v_packed = (__pyx_v_packed + 1);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":69 */
-      (__pyx_v_dna[3]) = (__pyx_v_7_twobit_valToNt[(__pyx_v_partial & 3)]);
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":69
+ *             partial = packed[0]
+ *             packed = packed + 1;
+ *             dna[3] = valToNt[partial&3];             # <<<<<<<<<<<<<< 
+ *             partial = partial >> 2
+ *             dna[2] = valToNt[partial&3];
+ */
+      (__pyx_v_dna[3]) = (__pyx_v_2bx_3seq_7_twobit_valToNt[(__pyx_v_partial & 3)]);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":70 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":70
+ *             packed = packed + 1;
+ *             dna[3] = valToNt[partial&3];
+ *             partial = partial >> 2             # <<<<<<<<<<<<<< 
+ *             dna[2] = valToNt[partial&3];
+ *             partial = partial >> 2
+ */
       __pyx_v_partial = (__pyx_v_partial >> 2);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":71 */
-      (__pyx_v_dna[2]) = (__pyx_v_7_twobit_valToNt[(__pyx_v_partial & 3)]);
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":71
+ *             dna[3] = valToNt[partial&3];
+ *             partial = partial >> 2
+ *             dna[2] = valToNt[partial&3];             # <<<<<<<<<<<<<< 
+ *             partial = partial >> 2
+ *             dna[1] = valToNt[partial&3];
+ */
+      (__pyx_v_dna[2]) = (__pyx_v_2bx_3seq_7_twobit_valToNt[(__pyx_v_partial & 3)]);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":72 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":72
+ *             partial = partial >> 2
+ *             dna[2] = valToNt[partial&3];
+ *             partial = partial >> 2             # <<<<<<<<<<<<<< 
+ *             dna[1] = valToNt[partial&3];
+ *             partial = partial >> 2
+ */
       __pyx_v_partial = (__pyx_v_partial >> 2);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":73 */
-      (__pyx_v_dna[1]) = (__pyx_v_7_twobit_valToNt[(__pyx_v_partial & 3)]);
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":73
+ *             dna[2] = valToNt[partial&3];
+ *             partial = partial >> 2
+ *             dna[1] = valToNt[partial&3];             # <<<<<<<<<<<<<< 
+ *             partial = partial >> 2
+ *             dna[0] = valToNt[partial&3];
+ */
+      (__pyx_v_dna[1]) = (__pyx_v_2bx_3seq_7_twobit_valToNt[(__pyx_v_partial & 3)]);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":74 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":74
+ *             partial = partial >> 2
+ *             dna[1] = valToNt[partial&3];
+ *             partial = partial >> 2             # <<<<<<<<<<<<<< 
+ *             dna[0] = valToNt[partial&3];
+ *             dna = dna + 4;            
+ */
       __pyx_v_partial = (__pyx_v_partial >> 2);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":75 */
-      (__pyx_v_dna[0]) = (__pyx_v_7_twobit_valToNt[(__pyx_v_partial & 3)]);
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":75
+ *             dna[1] = valToNt[partial&3];
+ *             partial = partial >> 2
+ *             dna[0] = valToNt[partial&3];             # <<<<<<<<<<<<<< 
+ *             dna = dna + 4;            
+ *             # Increment
+ */
+      (__pyx_v_dna[0]) = (__pyx_v_2bx_3seq_7_twobit_valToNt[(__pyx_v_partial & 3)]);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":76 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":76
+ *             partial = partial >> 2
+ *             dna[0] = valToNt[partial&3];
+ *             dna = dna + 4;             # <<<<<<<<<<<<<< 
+ *             # Increment
+ *             i = i + 4
+ */
       __pyx_v_dna = (__pyx_v_dna + 4);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":78 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":78
+ *             dna = dna + 4;            
+ *             # Increment
+ *             i = i + 4             # <<<<<<<<<<<<<< 
+ *             ## sys.stderr.write( "!!!< " + dna_py + " >!!!\n" ); sys.stderr.flush()
+ *         # End
+ */
       __pyx_v_i = (__pyx_v_i + 4);
     }
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":81 */
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":81
+ *             ## sys.stderr.write( "!!!< " + dna_py + " >!!!\n" ); sys.stderr.flush()
+ *         # End
+ *         if remainder > 0:             # <<<<<<<<<<<<<< 
+ *             partial = packed[0];
+ *             partial = partial >> (8-remainder-remainder)
+ */
     __pyx_5 = (__pyx_v_remainder > 0);
     if (__pyx_5) {
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":82 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":82
+ *         # End
+ *         if remainder > 0:
+ *             partial = packed[0];             # <<<<<<<<<<<<<< 
+ *             partial = partial >> (8-remainder-remainder)
+ *             for i from remainder - 1 >= i >= 0:
+ */
       __pyx_v_partial = (__pyx_v_packed[0]);
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":83 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":83
+ *         if remainder > 0:
+ *             partial = packed[0];
+ *             partial = partial >> (8-remainder-remainder)             # <<<<<<<<<<<<<< 
+ *             for i from remainder - 1 >= i >= 0:
+ *                 dna[i] = valToNt[partial&3]
+ */
       __pyx_v_partial = (__pyx_v_partial >> ((8 - __pyx_v_remainder) - __pyx_v_remainder));
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":84 */
-      for (__pyx_v_i = (__pyx_v_remainder - 1); __pyx_v_i >= 0; --__pyx_v_i) {
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":84
+ *             partial = packed[0];
+ *             partial = partial >> (8-remainder-remainder)
+ *             for i from remainder - 1 >= i >= 0:             # <<<<<<<<<<<<<< 
+ *                 dna[i] = valToNt[partial&3]
+ *                 partial = partial >> 2
+ */
+      for (__pyx_v_i = (__pyx_v_remainder - 1); __pyx_v_i >= 0; __pyx_v_i--) {
 
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":85 */
-        (__pyx_v_dna[__pyx_v_i]) = (__pyx_v_7_twobit_valToNt[(__pyx_v_partial & 3)]);
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":85
+ *             partial = partial >> (8-remainder-remainder)
+ *             for i from remainder - 1 >= i >= 0:
+ *                 dna[i] = valToNt[partial&3]             # <<<<<<<<<<<<<< 
+ *                 partial = partial >> 2
+ *     # Restore DNA pointer
+ */
+        (__pyx_v_dna[__pyx_v_i]) = (__pyx_v_2bx_3seq_7_twobit_valToNt[(__pyx_v_partial & 3)]);
 
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":86 */
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":86
+ *             for i from remainder - 1 >= i >= 0:
+ *                 dna[i] = valToNt[partial&3]
+ *                 partial = partial >> 2             # <<<<<<<<<<<<<< 
+ *     # Restore DNA pointer
+ *     dna = PyString_AsString( dna_py )
+ */
         __pyx_v_partial = (__pyx_v_partial >> 2);
       }
-      goto __pyx_L10;
+      goto __pyx_L12;
     }
-    __pyx_L10:;
+    __pyx_L12:;
   }
-  __pyx_L2:;
+  __pyx_L4:;
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":88 */
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":88
+ *                 partial = partial >> 2
+ *     # Restore DNA pointer
+ *     dna = PyString_AsString( dna_py )             # <<<<<<<<<<<<<< 
+ *     # N's
+ *     n_block_count = len( seq.n_block_starts )
+ */
   __pyx_v_dna = PyString_AsString(__pyx_v_dna_py);
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":90 */
-  __pyx_2 = __Pyx_GetName(__pyx_b, __pyx_n_len); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; goto __pyx_L1;}
-  __pyx_3 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_n_block_starts); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; goto __pyx_L1;}
-  __pyx_1 = PyTuple_New(1); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; goto __pyx_L1;}
-  PyTuple_SET_ITEM(__pyx_1, 0, __pyx_3);
-  __pyx_3 = 0;
-  __pyx_4 = PyObject_CallObject(__pyx_2, __pyx_1); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; goto __pyx_L1;}
-  Py_DECREF(__pyx_2); __pyx_2 = 0;
-  Py_DECREF(__pyx_1); __pyx_1 = 0;
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":90
+ *     dna = PyString_AsString( dna_py )
+ *     # N's
+ *     n_block_count = len( seq.n_block_starts )             # <<<<<<<<<<<<<< 
+ *     if n_block_count > 0:
+ *         start_ix = bisect( seq.n_block_starts, fragStart ) - 1
+ */
+  __pyx_3 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_n_block_starts); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  __pyx_6 = PyObject_Length(__pyx_3); if (unlikely(__pyx_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  Py_DECREF(__pyx_3); __pyx_3 = 0;
+  __pyx_1 = PyInt_FromSsize_t(__pyx_6); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1;}
   Py_DECREF(__pyx_v_n_block_count);
-  __pyx_v_n_block_count = __pyx_4;
-  __pyx_4 = 0;
+  __pyx_v_n_block_count = __pyx_1;
+  __pyx_1 = 0;
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":91 */
-  __pyx_3 = PyInt_FromLong(0); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 91; goto __pyx_L1;}
-  if (PyObject_Cmp(__pyx_v_n_block_count, __pyx_3, &__pyx_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 91; goto __pyx_L1;}
-  __pyx_5 = __pyx_5 > 0;
-  Py_DECREF(__pyx_3); __pyx_3 = 0;
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":91
+ *     # N's
+ *     n_block_count = len( seq.n_block_starts )
+ *     if n_block_count > 0:             # <<<<<<<<<<<<<< 
+ *         start_ix = bisect( seq.n_block_starts, fragStart ) - 1
+ *         if start_ix < 0: start_ix = 0            
+ */
+  __pyx_4 = PyObject_RichCompare(__pyx_v_n_block_count, __pyx_int_0, Py_GT); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 91; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  __pyx_5 = __Pyx_PyObject_IsTrue(__pyx_4); if (unlikely(__pyx_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 91; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  Py_DECREF(__pyx_4); __pyx_4 = 0;
   if (__pyx_5) {
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":92 */
-    __pyx_2 = __Pyx_GetName(__pyx_m, __pyx_n_bisect); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; goto __pyx_L1;}
-    __pyx_1 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_n_block_starts); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; goto __pyx_L1;}
-    __pyx_4 = PyInt_FromLong(__pyx_v_fragStart); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; goto __pyx_L1;}
-    __pyx_3 = PyTuple_New(2); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; goto __pyx_L1;}
-    PyTuple_SET_ITEM(__pyx_3, 0, __pyx_1);
-    PyTuple_SET_ITEM(__pyx_3, 1, __pyx_4);
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":92
+ *     n_block_count = len( seq.n_block_starts )
+ *     if n_block_count > 0:
+ *         start_ix = bisect( seq.n_block_starts, fragStart ) - 1             # <<<<<<<<<<<<<< 
+ *         if start_ix < 0: start_ix = 0            
+ *         for i from start_ix <= i < n_block_count:
+ */
+    __pyx_2 = __Pyx_GetName(__pyx_m, __pyx_n_bisect); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1;}
+    __pyx_3 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_n_block_starts); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1;}
+    __pyx_1 = PyInt_FromLong(__pyx_v_fragStart); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1;}
+    __pyx_4 = PyTuple_New(2); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1;}
+    PyTuple_SET_ITEM(__pyx_4, 0, __pyx_3);
+    PyTuple_SET_ITEM(__pyx_4, 1, __pyx_1);
+    __pyx_3 = 0;
     __pyx_1 = 0;
-    __pyx_4 = 0;
-    __pyx_1 = PyObject_CallObject(__pyx_2, __pyx_3); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; goto __pyx_L1;}
+    __pyx_3 = PyObject_Call(__pyx_2, ((PyObject *)__pyx_4), NULL); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1;}
     Py_DECREF(__pyx_2); __pyx_2 = 0;
+    Py_DECREF(((PyObject *)__pyx_4)); __pyx_4 = 0;
+    __pyx_1 = PyNumber_Subtract(__pyx_3, __pyx_int_1); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1;}
     Py_DECREF(__pyx_3); __pyx_3 = 0;
-    __pyx_4 = PyInt_FromLong(1); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; goto __pyx_L1;}
-    __pyx_2 = PyNumber_Subtract(__pyx_1, __pyx_4); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; goto __pyx_L1;}
-    Py_DECREF(__pyx_1); __pyx_1 = 0;
-    Py_DECREF(__pyx_4); __pyx_4 = 0;
     Py_DECREF(__pyx_v_start_ix);
-    __pyx_v_start_ix = __pyx_2;
-    __pyx_2 = 0;
+    __pyx_v_start_ix = __pyx_1;
+    __pyx_1 = 0;
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":93 */
-    __pyx_3 = PyInt_FromLong(0); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 93; goto __pyx_L1;}
-    if (PyObject_Cmp(__pyx_v_start_ix, __pyx_3, &__pyx_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 93; goto __pyx_L1;}
-    __pyx_5 = __pyx_5 < 0;
-    Py_DECREF(__pyx_3); __pyx_3 = 0;
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":93
+ *     if n_block_count > 0:
+ *         start_ix = bisect( seq.n_block_starts, fragStart ) - 1
+ *         if start_ix < 0: start_ix = 0             # <<<<<<<<<<<<<< 
+ *         for i from start_ix <= i < n_block_count:
+ *             s = seq.n_block_starts[i];
+ */
+    __pyx_2 = PyObject_RichCompare(__pyx_v_start_ix, __pyx_int_0, Py_LT); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 93; __pyx_clineno = __LINE__; goto __pyx_L1;}
+    __pyx_5 = __Pyx_PyObject_IsTrue(__pyx_2); if (unlikely(__pyx_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 93; __pyx_clineno = __LINE__; goto __pyx_L1;}
+    Py_DECREF(__pyx_2); __pyx_2 = 0;
     if (__pyx_5) {
-      __pyx_1 = PyInt_FromLong(0); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 93; goto __pyx_L1;}
+      Py_INCREF(__pyx_int_0);
       Py_DECREF(__pyx_v_start_ix);
-      __pyx_v_start_ix = __pyx_1;
-      __pyx_1 = 0;
-      goto __pyx_L14;
+      __pyx_v_start_ix = __pyx_int_0;
+      goto __pyx_L16;
     }
-    __pyx_L14:;
+    __pyx_L16:;
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":94 */
-    __pyx_6 = PyInt_AsLong(__pyx_v_start_ix); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 94; goto __pyx_L1;}
-    __pyx_7 = PyInt_AsLong(__pyx_v_n_block_count); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 94; goto __pyx_L1;}
-    for (__pyx_v_i = __pyx_6; __pyx_v_i < __pyx_7; ++__pyx_v_i) {
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":94
+ *         start_ix = bisect( seq.n_block_starts, fragStart ) - 1
+ *         if start_ix < 0: start_ix = 0            
+ *         for i from start_ix <= i < n_block_count:             # <<<<<<<<<<<<<< 
+ *             s = seq.n_block_starts[i];
+ *             e = s + seq.n_block_sizes[i];
+ */
+    __pyx_7 = __pyx_PyInt_int(__pyx_v_start_ix); if (unlikely((__pyx_7 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 94; __pyx_clineno = __LINE__; goto __pyx_L1;}
+    __pyx_8 = __pyx_PyInt_int(__pyx_v_n_block_count); if (unlikely((__pyx_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 94; __pyx_clineno = __LINE__; goto __pyx_L1;}
+    for (__pyx_v_i = __pyx_7; __pyx_v_i < __pyx_8; __pyx_v_i++) {
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":95 */
-      __pyx_4 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_n_block_starts); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; goto __pyx_L1;}
-      __pyx_2 = PyInt_FromLong(__pyx_v_i); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; goto __pyx_L1;}
-      __pyx_3 = PyObject_GetItem(__pyx_4, __pyx_2); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; goto __pyx_L1;}
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":95
+ *         if start_ix < 0: start_ix = 0            
+ *         for i from start_ix <= i < n_block_count:
+ *             s = seq.n_block_starts[i];             # <<<<<<<<<<<<<< 
+ *             e = s + seq.n_block_sizes[i];
+ *             if (s >= fragEnd):
+ */
+      __pyx_4 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_n_block_starts); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      if (PyList_CheckExact(__pyx_4) && 0 <= __pyx_v_i && __pyx_v_i < PyList_GET_SIZE(__pyx_4)) {
+        __pyx_1 = PyList_GET_ITEM(__pyx_4, __pyx_v_i); Py_INCREF(__pyx_1);
+        } else if (PyTuple_CheckExact(__pyx_4) && 0 <= __pyx_v_i && __pyx_v_i < PyTuple_GET_SIZE(__pyx_4)) {
+        __pyx_1 = PyTuple_GET_ITEM(__pyx_4, __pyx_v_i); Py_INCREF(__pyx_1);
+        } else {
+        __pyx_3 = PyInt_FromLong(__pyx_v_i); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; __pyx_clineno = __LINE__; goto __pyx_L1;}
+        __pyx_1 = PyObject_GetItem(__pyx_4, __pyx_3); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; __pyx_clineno = __LINE__; goto __pyx_L1;}
+        Py_DECREF(__pyx_3); __pyx_3 = 0;
+      }
       Py_DECREF(__pyx_4); __pyx_4 = 0;
+      __pyx_9 = __pyx_PyInt_int(__pyx_1); if (unlikely((__pyx_9 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      Py_DECREF(__pyx_1); __pyx_1 = 0;
+      __pyx_v_s = __pyx_9;
+
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":96
+ *         for i from start_ix <= i < n_block_count:
+ *             s = seq.n_block_starts[i];
+ *             e = s + seq.n_block_sizes[i];             # <<<<<<<<<<<<<< 
+ *             if (s >= fragEnd):
+ *                 break
+ */
+      __pyx_2 = PyInt_FromLong(__pyx_v_s); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      __pyx_4 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_n_block_sizes); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      if (PyList_CheckExact(__pyx_4) && 0 <= __pyx_v_i && __pyx_v_i < PyList_GET_SIZE(__pyx_4)) {
+        __pyx_1 = PyList_GET_ITEM(__pyx_4, __pyx_v_i); Py_INCREF(__pyx_1);
+        } else if (PyTuple_CheckExact(__pyx_4) && 0 <= __pyx_v_i && __pyx_v_i < PyTuple_GET_SIZE(__pyx_4)) {
+        __pyx_1 = PyTuple_GET_ITEM(__pyx_4, __pyx_v_i); Py_INCREF(__pyx_1);
+        } else {
+        __pyx_3 = PyInt_FromLong(__pyx_v_i); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; __pyx_clineno = __LINE__; goto __pyx_L1;}
+        __pyx_1 = PyObject_GetItem(__pyx_4, __pyx_3); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; __pyx_clineno = __LINE__; goto __pyx_L1;}
+        Py_DECREF(__pyx_3); __pyx_3 = 0;
+      }
+      Py_DECREF(__pyx_4); __pyx_4 = 0;
+      __pyx_4 = PyNumber_Add(__pyx_2, __pyx_1); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; __pyx_clineno = __LINE__; goto __pyx_L1;}
       Py_DECREF(__pyx_2); __pyx_2 = 0;
-      __pyx_5 = PyInt_AsLong(__pyx_3); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 95; goto __pyx_L1;}
-      Py_DECREF(__pyx_3); __pyx_3 = 0;
-      __pyx_v_s = __pyx_5;
+      Py_DECREF(__pyx_1); __pyx_1 = 0;
+      __pyx_9 = __pyx_PyInt_int(__pyx_4); if (unlikely((__pyx_9 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      Py_DECREF(__pyx_4); __pyx_4 = 0;
+      __pyx_v_e = __pyx_9;
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":96 */
-      __pyx_1 = PyInt_FromLong(__pyx_v_s); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; goto __pyx_L1;}
-      __pyx_4 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_n_block_sizes); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; goto __pyx_L1;}
-      __pyx_2 = PyInt_FromLong(__pyx_v_i); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; goto __pyx_L1;}
-      __pyx_3 = PyObject_GetItem(__pyx_4, __pyx_2); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; goto __pyx_L1;}
-      Py_DECREF(__pyx_4); __pyx_4 = 0;
-      Py_DECREF(__pyx_2); __pyx_2 = 0;
-      __pyx_4 = PyNumber_Add(__pyx_1, __pyx_3); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; goto __pyx_L1;}
-      Py_DECREF(__pyx_1); __pyx_1 = 0;
-      Py_DECREF(__pyx_3); __pyx_3 = 0;
-      __pyx_5 = PyInt_AsLong(__pyx_4); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 96; goto __pyx_L1;}
-      Py_DECREF(__pyx_4); __pyx_4 = 0;
-      __pyx_v_e = __pyx_5;
-
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":97 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":97
+ *             s = seq.n_block_starts[i];
+ *             e = s + seq.n_block_sizes[i];
+ *             if (s >= fragEnd):             # <<<<<<<<<<<<<< 
+ *                 break
+ *             if (s < fragStart):
+ */
       __pyx_5 = (__pyx_v_s >= __pyx_v_fragEnd);
       if (__pyx_5) {
-
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":98 */
-        goto __pyx_L16;
-        goto __pyx_L17;
-      }
-      __pyx_L17:;
-
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":99 */
-      __pyx_5 = (__pyx_v_s < __pyx_v_fragStart);
-      if (__pyx_5) {
-
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":100 */
-        __pyx_v_s = __pyx_v_fragStart;
         goto __pyx_L18;
-      }
-      __pyx_L18:;
-
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":101 */
-      __pyx_5 = (__pyx_v_e > __pyx_v_fragEnd);
-      if (__pyx_5) {
-
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":102 */
-        __pyx_v_e = __pyx_v_fragEnd;
         goto __pyx_L19;
       }
       __pyx_L19:;
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":103 */
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":99
+ *             if (s >= fragEnd):
+ *                 break
+ *             if (s < fragStart):             # <<<<<<<<<<<<<< 
+ *                s = fragStart
+ *             if (e > fragEnd):
+ */
+      __pyx_5 = (__pyx_v_s < __pyx_v_fragStart);
+      if (__pyx_5) {
+
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":100
+ *                 break
+ *             if (s < fragStart):
+ *                s = fragStart             # <<<<<<<<<<<<<< 
+ *             if (e > fragEnd):
+ *                e = fragEnd
+ */
+        __pyx_v_s = __pyx_v_fragStart;
+        goto __pyx_L20;
+      }
+      __pyx_L20:;
+
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":101
+ *             if (s < fragStart):
+ *                s = fragStart
+ *             if (e > fragEnd):             # <<<<<<<<<<<<<< 
+ *                e = fragEnd
+ *             if (s < e):
+ */
+      __pyx_5 = (__pyx_v_e > __pyx_v_fragEnd);
+      if (__pyx_5) {
+
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":102
+ *                s = fragStart
+ *             if (e > fragEnd):
+ *                e = fragEnd             # <<<<<<<<<<<<<< 
+ *             if (s < e):
+ *                 memset( dna + s - fragStart, 'N'[0], e - s)
+ */
+        __pyx_v_e = __pyx_v_fragEnd;
+        goto __pyx_L21;
+      }
+      __pyx_L21:;
+
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":103
+ *             if (e > fragEnd):
+ *                e = fragEnd
+ *             if (s < e):             # <<<<<<<<<<<<<< 
+ *                 memset( dna + s - fragStart, 'N'[0], e - s)
+ *     # Mask
+ */
       __pyx_5 = (__pyx_v_s < __pyx_v_e);
       if (__pyx_5) {
 
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":104 */
-        memset(((__pyx_v_dna + __pyx_v_s) - __pyx_v_fragStart),(__pyx_k7[0]),(__pyx_v_e - __pyx_v_s));
-        goto __pyx_L20;
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":104
+ *                e = fragEnd
+ *             if (s < e):
+ *                 memset( dna + s - fragStart, 'N'[0], e - s)             # <<<<<<<<<<<<<< 
+ *     # Mask
+ *     if do_mask:
+ */
+        memset(((__pyx_v_dna + __pyx_v_s) - __pyx_v_fragStart), (__pyx_k_6[0]), (__pyx_v_e - __pyx_v_s));
+        goto __pyx_L22;
       }
-      __pyx_L20:;
+      __pyx_L22:;
     }
-    __pyx_L16:;
-    goto __pyx_L13;
+    __pyx_L18:;
+    goto __pyx_L15;
   }
-  __pyx_L13:;
+  __pyx_L15:;
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":106 */
-  __pyx_5 = PyObject_IsTrue(__pyx_v_do_mask); if (__pyx_5 < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 106; goto __pyx_L1;}
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":106
+ *                 memset( dna + s - fragStart, 'N'[0], e - s)
+ *     # Mask
+ *     if do_mask:             # <<<<<<<<<<<<<< 
+ *         m_block_count = len( seq.masked_block_starts )
+ *         if m_block_count > 0:
+ */
+  __pyx_5 = __Pyx_PyObject_IsTrue(__pyx_v_do_mask); if (unlikely(__pyx_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 106; __pyx_clineno = __LINE__; goto __pyx_L1;}
   if (__pyx_5) {
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":107 */
-    __pyx_2 = __Pyx_GetName(__pyx_b, __pyx_n_len); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; goto __pyx_L1;}
-    __pyx_1 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_masked_block_starts); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; goto __pyx_L1;}
-    __pyx_3 = PyTuple_New(1); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; goto __pyx_L1;}
-    PyTuple_SET_ITEM(__pyx_3, 0, __pyx_1);
-    __pyx_1 = 0;
-    __pyx_4 = PyObject_CallObject(__pyx_2, __pyx_3); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; goto __pyx_L1;}
-    Py_DECREF(__pyx_2); __pyx_2 = 0;
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":107
+ *     # Mask
+ *     if do_mask:
+ *         m_block_count = len( seq.masked_block_starts )             # <<<<<<<<<<<<<< 
+ *         if m_block_count > 0:
+ *             start_ix = bisect( seq.masked_block_starts, fragStart ) - 1
+ */
+    __pyx_3 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_masked_block_starts); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1;}
+    __pyx_6 = PyObject_Length(__pyx_3); if (unlikely(__pyx_6 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1;}
     Py_DECREF(__pyx_3); __pyx_3 = 0;
+    __pyx_2 = PyInt_FromSsize_t(__pyx_6); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1;}
     Py_DECREF(__pyx_v_m_block_count);
-    __pyx_v_m_block_count = __pyx_4;
-    __pyx_4 = 0;
+    __pyx_v_m_block_count = __pyx_2;
+    __pyx_2 = 0;
 
-    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":108 */
-    __pyx_1 = PyInt_FromLong(0); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; goto __pyx_L1;}
-    if (PyObject_Cmp(__pyx_v_m_block_count, __pyx_1, &__pyx_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; goto __pyx_L1;}
-    __pyx_5 = __pyx_5 > 0;
+    /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":108
+ *     if do_mask:
+ *         m_block_count = len( seq.masked_block_starts )
+ *         if m_block_count > 0:             # <<<<<<<<<<<<<< 
+ *             start_ix = bisect( seq.masked_block_starts, fragStart ) - 1
+ *             if start_ix < 0: start_ix = 0    
+ */
+    __pyx_1 = PyObject_RichCompare(__pyx_v_m_block_count, __pyx_int_0, Py_GT); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1;}
+    __pyx_5 = __Pyx_PyObject_IsTrue(__pyx_1); if (unlikely(__pyx_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L1;}
     Py_DECREF(__pyx_1); __pyx_1 = 0;
     if (__pyx_5) {
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":109 */
-      __pyx_2 = __Pyx_GetName(__pyx_m, __pyx_n_bisect); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; goto __pyx_L1;}
-      __pyx_3 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_masked_block_starts); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; goto __pyx_L1;}
-      __pyx_4 = PyInt_FromLong(__pyx_v_fragStart); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; goto __pyx_L1;}
-      __pyx_1 = PyTuple_New(2); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; goto __pyx_L1;}
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":109
+ *         m_block_count = len( seq.masked_block_starts )
+ *         if m_block_count > 0:
+ *             start_ix = bisect( seq.masked_block_starts, fragStart ) - 1             # <<<<<<<<<<<<<< 
+ *             if start_ix < 0: start_ix = 0    
+ *             for i from start_ix <= i < m_block_count:
+ */
+      __pyx_4 = __Pyx_GetName(__pyx_m, __pyx_n_bisect); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      __pyx_3 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_masked_block_starts); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      __pyx_2 = PyInt_FromLong(__pyx_v_fragStart); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      __pyx_1 = PyTuple_New(2); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto __pyx_L1;}
       PyTuple_SET_ITEM(__pyx_1, 0, __pyx_3);
-      PyTuple_SET_ITEM(__pyx_1, 1, __pyx_4);
+      PyTuple_SET_ITEM(__pyx_1, 1, __pyx_2);
       __pyx_3 = 0;
-      __pyx_4 = 0;
-      __pyx_3 = PyObject_CallObject(__pyx_2, __pyx_1); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; goto __pyx_L1;}
-      Py_DECREF(__pyx_2); __pyx_2 = 0;
-      Py_DECREF(__pyx_1); __pyx_1 = 0;
-      __pyx_4 = PyInt_FromLong(1); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; goto __pyx_L1;}
-      __pyx_2 = PyNumber_Subtract(__pyx_3, __pyx_4); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; goto __pyx_L1;}
+      __pyx_2 = 0;
+      __pyx_3 = PyObject_Call(__pyx_4, ((PyObject *)__pyx_1), NULL); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      Py_DECREF(__pyx_4); __pyx_4 = 0;
+      Py_DECREF(((PyObject *)__pyx_1)); __pyx_1 = 0;
+      __pyx_2 = PyNumber_Subtract(__pyx_3, __pyx_int_1); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto __pyx_L1;}
       Py_DECREF(__pyx_3); __pyx_3 = 0;
-      Py_DECREF(__pyx_4); __pyx_4 = 0;
       Py_DECREF(__pyx_v_start_ix);
       __pyx_v_start_ix = __pyx_2;
       __pyx_2 = 0;
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":110 */
-      __pyx_1 = PyInt_FromLong(0); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; goto __pyx_L1;}
-      if (PyObject_Cmp(__pyx_v_start_ix, __pyx_1, &__pyx_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; goto __pyx_L1;}
-      __pyx_5 = __pyx_5 < 0;
-      Py_DECREF(__pyx_1); __pyx_1 = 0;
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":110
+ *         if m_block_count > 0:
+ *             start_ix = bisect( seq.masked_block_starts, fragStart ) - 1
+ *             if start_ix < 0: start_ix = 0             # <<<<<<<<<<<<<< 
+ *             for i from start_ix <= i < m_block_count:
+ *                 s = seq.masked_block_starts[i];
+ */
+      __pyx_4 = PyObject_RichCompare(__pyx_v_start_ix, __pyx_int_0, Py_LT); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      __pyx_5 = __Pyx_PyObject_IsTrue(__pyx_4); if (unlikely(__pyx_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      Py_DECREF(__pyx_4); __pyx_4 = 0;
       if (__pyx_5) {
-        __pyx_3 = PyInt_FromLong(0); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 110; goto __pyx_L1;}
+        Py_INCREF(__pyx_int_0);
         Py_DECREF(__pyx_v_start_ix);
-        __pyx_v_start_ix = __pyx_3;
-        __pyx_3 = 0;
-        goto __pyx_L23;
+        __pyx_v_start_ix = __pyx_int_0;
+        goto __pyx_L25;
       }
-      __pyx_L23:;
+      __pyx_L25:;
 
-      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":111 */
-      __pyx_6 = PyInt_AsLong(__pyx_v_start_ix); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; goto __pyx_L1;}
-      __pyx_7 = PyInt_AsLong(__pyx_v_m_block_count); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; goto __pyx_L1;}
-      for (__pyx_v_i = __pyx_6; __pyx_v_i < __pyx_7; ++__pyx_v_i) {
+      /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":111
+ *             start_ix = bisect( seq.masked_block_starts, fragStart ) - 1
+ *             if start_ix < 0: start_ix = 0    
+ *             for i from start_ix <= i < m_block_count:             # <<<<<<<<<<<<<< 
+ *                 s = seq.masked_block_starts[i];
+ *                 e = s + seq.masked_block_sizes[i];
+ */
+      __pyx_9 = __pyx_PyInt_int(__pyx_v_start_ix); if (unlikely((__pyx_9 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      __pyx_7 = __pyx_PyInt_int(__pyx_v_m_block_count); if (unlikely((__pyx_7 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L1;}
+      for (__pyx_v_i = __pyx_9; __pyx_v_i < __pyx_7; __pyx_v_i++) {
 
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":112 */
-        __pyx_4 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_masked_block_starts); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; goto __pyx_L1;}
-        __pyx_2 = PyInt_FromLong(__pyx_v_i); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; goto __pyx_L1;}
-        __pyx_1 = PyObject_GetItem(__pyx_4, __pyx_2); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; goto __pyx_L1;}
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":112
+ *             if start_ix < 0: start_ix = 0    
+ *             for i from start_ix <= i < m_block_count:
+ *                 s = seq.masked_block_starts[i];             # <<<<<<<<<<<<<< 
+ *                 e = s + seq.masked_block_sizes[i];
+ *                 if (s >= fragEnd):
+ */
+        __pyx_1 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_masked_block_starts); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1;}
+        if (PyList_CheckExact(__pyx_1) && 0 <= __pyx_v_i && __pyx_v_i < PyList_GET_SIZE(__pyx_1)) {
+          __pyx_2 = PyList_GET_ITEM(__pyx_1, __pyx_v_i); Py_INCREF(__pyx_2);
+          } else if (PyTuple_CheckExact(__pyx_1) && 0 <= __pyx_v_i && __pyx_v_i < PyTuple_GET_SIZE(__pyx_1)) {
+          __pyx_2 = PyTuple_GET_ITEM(__pyx_1, __pyx_v_i); Py_INCREF(__pyx_2);
+          } else {
+          __pyx_3 = PyInt_FromLong(__pyx_v_i); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1;}
+          __pyx_2 = PyObject_GetItem(__pyx_1, __pyx_3); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1;}
+          Py_DECREF(__pyx_3); __pyx_3 = 0;
+        }
+        Py_DECREF(__pyx_1); __pyx_1 = 0;
+        __pyx_8 = __pyx_PyInt_int(__pyx_2); if (unlikely((__pyx_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; __pyx_clineno = __LINE__; goto __pyx_L1;}
+        Py_DECREF(__pyx_2); __pyx_2 = 0;
+        __pyx_v_s = __pyx_8;
+
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":113
+ *             for i from start_ix <= i < m_block_count:
+ *                 s = seq.masked_block_starts[i];
+ *                 e = s + seq.masked_block_sizes[i];             # <<<<<<<<<<<<<< 
+ *                 if (s >= fragEnd):
+ *                     break
+ */
+        __pyx_4 = PyInt_FromLong(__pyx_v_s); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1;}
+        __pyx_1 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_masked_block_sizes); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1;}
+        if (PyList_CheckExact(__pyx_1) && 0 <= __pyx_v_i && __pyx_v_i < PyList_GET_SIZE(__pyx_1)) {
+          __pyx_2 = PyList_GET_ITEM(__pyx_1, __pyx_v_i); Py_INCREF(__pyx_2);
+          } else if (PyTuple_CheckExact(__pyx_1) && 0 <= __pyx_v_i && __pyx_v_i < PyTuple_GET_SIZE(__pyx_1)) {
+          __pyx_2 = PyTuple_GET_ITEM(__pyx_1, __pyx_v_i); Py_INCREF(__pyx_2);
+          } else {
+          __pyx_3 = PyInt_FromLong(__pyx_v_i); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1;}
+          __pyx_2 = PyObject_GetItem(__pyx_1, __pyx_3); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1;}
+          Py_DECREF(__pyx_3); __pyx_3 = 0;
+        }
+        Py_DECREF(__pyx_1); __pyx_1 = 0;
+        __pyx_1 = PyNumber_Add(__pyx_4, __pyx_2); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1;}
         Py_DECREF(__pyx_4); __pyx_4 = 0;
         Py_DECREF(__pyx_2); __pyx_2 = 0;
-        __pyx_5 = PyInt_AsLong(__pyx_1); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 112; goto __pyx_L1;}
+        __pyx_8 = __pyx_PyInt_int(__pyx_1); if (unlikely((__pyx_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; __pyx_clineno = __LINE__; goto __pyx_L1;}
         Py_DECREF(__pyx_1); __pyx_1 = 0;
-        __pyx_v_s = __pyx_5;
+        __pyx_v_e = __pyx_8;
 
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":113 */
-        __pyx_3 = PyInt_FromLong(__pyx_v_s); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; goto __pyx_L1;}
-        __pyx_4 = PyObject_GetAttr(__pyx_v_seq, __pyx_n_masked_block_sizes); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; goto __pyx_L1;}
-        __pyx_2 = PyInt_FromLong(__pyx_v_i); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; goto __pyx_L1;}
-        __pyx_1 = PyObject_GetItem(__pyx_4, __pyx_2); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; goto __pyx_L1;}
-        Py_DECREF(__pyx_4); __pyx_4 = 0;
-        Py_DECREF(__pyx_2); __pyx_2 = 0;
-        __pyx_4 = PyNumber_Add(__pyx_3, __pyx_1); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; goto __pyx_L1;}
-        Py_DECREF(__pyx_3); __pyx_3 = 0;
-        Py_DECREF(__pyx_1); __pyx_1 = 0;
-        __pyx_5 = PyInt_AsLong(__pyx_4); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 113; goto __pyx_L1;}
-        Py_DECREF(__pyx_4); __pyx_4 = 0;
-        __pyx_v_e = __pyx_5;
-
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":114 */
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":114
+ *                 s = seq.masked_block_starts[i];
+ *                 e = s + seq.masked_block_sizes[i];
+ *                 if (s >= fragEnd):             # <<<<<<<<<<<<<< 
+ *                     break
+ *                 if (s < fragStart):
+ */
         __pyx_5 = (__pyx_v_s >= __pyx_v_fragEnd);
         if (__pyx_5) {
-
-          /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":115 */
-          goto __pyx_L25;
-          goto __pyx_L26;
-        }
-        __pyx_L26:;
-
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":116 */
-        __pyx_5 = (__pyx_v_s < __pyx_v_fragStart);
-        if (__pyx_5) {
-
-          /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":117 */
-          __pyx_v_s = __pyx_v_fragStart;
           goto __pyx_L27;
-        }
-        __pyx_L27:;
-
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":118 */
-        __pyx_5 = (__pyx_v_e > __pyx_v_fragEnd);
-        if (__pyx_5) {
-
-          /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":119 */
-          __pyx_v_e = __pyx_v_fragEnd;
           goto __pyx_L28;
         }
         __pyx_L28:;
 
-        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":120 */
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":116
+ *                 if (s >= fragEnd):
+ *                     break
+ *                 if (s < fragStart):             # <<<<<<<<<<<<<< 
+ *                    s = fragStart
+ *                 if (e > fragEnd):
+ */
+        __pyx_5 = (__pyx_v_s < __pyx_v_fragStart);
+        if (__pyx_5) {
+
+          /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":117
+ *                     break
+ *                 if (s < fragStart):
+ *                    s = fragStart             # <<<<<<<<<<<<<< 
+ *                 if (e > fragEnd):
+ *                    e = fragEnd
+ */
+          __pyx_v_s = __pyx_v_fragStart;
+          goto __pyx_L29;
+        }
+        __pyx_L29:;
+
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":118
+ *                 if (s < fragStart):
+ *                    s = fragStart
+ *                 if (e > fragEnd):             # <<<<<<<<<<<<<< 
+ *                    e = fragEnd
+ *                 if (s < e):
+ */
+        __pyx_5 = (__pyx_v_e > __pyx_v_fragEnd);
+        if (__pyx_5) {
+
+          /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":119
+ *                    s = fragStart
+ *                 if (e > fragEnd):
+ *                    e = fragEnd             # <<<<<<<<<<<<<< 
+ *                 if (s < e):
+ *                     for j from s <= j < e:
+ */
+          __pyx_v_e = __pyx_v_fragEnd;
+          goto __pyx_L30;
+        }
+        __pyx_L30:;
+
+        /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":120
+ *                 if (e > fragEnd):
+ *                    e = fragEnd
+ *                 if (s < e):             # <<<<<<<<<<<<<< 
+ *                     for j from s <= j < e:
+ *                         dna[j-fragStart] = tolower( dna[j-fragStart] )
+ */
         __pyx_5 = (__pyx_v_s < __pyx_v_e);
         if (__pyx_5) {
 
-          /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":121 */
-          for (__pyx_v_j = __pyx_v_s; __pyx_v_j < __pyx_v_e; ++__pyx_v_j) {
+          /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":121
+ *                    e = fragEnd
+ *                 if (s < e):
+ *                     for j from s <= j < e:             # <<<<<<<<<<<<<< 
+ *                         dna[j-fragStart] = tolower( dna[j-fragStart] )
+ *     return dna_py
+ */
+          for (__pyx_v_j = __pyx_v_s; __pyx_v_j < __pyx_v_e; __pyx_v_j++) {
 
-            /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":122 */
+            /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":122
+ *                 if (s < e):
+ *                     for j from s <= j < e:
+ *                         dna[j-fragStart] = tolower( dna[j-fragStart] )             # <<<<<<<<<<<<<< 
+ *     return dna_py
+ */
             (__pyx_v_dna[(__pyx_v_j - __pyx_v_fragStart)]) = tolower((__pyx_v_dna[(__pyx_v_j - __pyx_v_fragStart)]));
           }
-          goto __pyx_L29;
+          goto __pyx_L31;
         }
-        __pyx_L29:;
+        __pyx_L31:;
       }
-      __pyx_L25:;
-      goto __pyx_L22;
+      __pyx_L27:;
+      goto __pyx_L24;
     }
-    __pyx_L22:;
-    goto __pyx_L21;
+    __pyx_L24:;
+    goto __pyx_L23;
   }
-  __pyx_L21:;
+  __pyx_L23:;
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":123 */
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":123
+ *                     for j from s <= j < e:
+ *                         dna[j-fragStart] = tolower( dna[j-fragStart] )
+ *     return dna_py             # <<<<<<<<<<<<<< 
+ */
   Py_INCREF(__pyx_v_dna_py);
   __pyx_r = __pyx_v_dna_py;
   goto __pyx_L0;
   Py_XDECREF(__pyx_2);
   Py_XDECREF(__pyx_3);
   Py_XDECREF(__pyx_4);
-  __Pyx_AddTraceback("_twobit.read");
-  __pyx_r = 0;
+  __Pyx_AddTraceback("bx.seq._twobit.read");
+  __pyx_r = NULL;
   __pyx_L0:;
   Py_DECREF(__pyx_v_dna_py);
   Py_DECREF(__pyx_v_packed_py);
   Py_DECREF(__pyx_v_n_block_count);
   Py_DECREF(__pyx_v_start_ix);
   Py_DECREF(__pyx_v_m_block_count);
-  Py_DECREF(__pyx_v_file);
-  Py_DECREF(__pyx_v_seq);
-  Py_DECREF(__pyx_v_do_mask);
   return __pyx_r;
 }
 
 static __Pyx_InternTabEntry __pyx_intern_tab[] = {
-  {&__pyx_n_False, "False"},
   {&__pyx_n_bisect, "bisect"},
-  {&__pyx_n_len, "len"},
   {&__pyx_n_masked_block_sizes, "masked_block_sizes"},
   {&__pyx_n_masked_block_starts, "masked_block_starts"},
   {&__pyx_n_n_block_sizes, "n_block_sizes"},
 };
 
 static struct PyMethodDef __pyx_methods[] = {
-  {"read", (PyCFunction)__pyx_f_7_twobit_read, METH_VARARGS|METH_KEYWORDS, __pyx_doc_7_twobit_read},
+  {"read", (PyCFunction)__pyx_pf_2bx_3seq_7_twobit_read, METH_VARARGS|METH_KEYWORDS, __pyx_doc_2bx_3seq_7_twobit_read},
   {0, 0, 0, 0}
 };
 
 PyMODINIT_FUNC init_twobit(void) {
   PyObject *__pyx_1 = 0;
   PyObject *__pyx_2 = 0;
+  /*--- Libary function declarations ---*/
   __pyx_init_filenames();
+  /*--- Module creation code ---*/
   __pyx_m = Py_InitModule4("_twobit", __pyx_methods, 0, 0, PYTHON_API_VERSION);
-  if (!__pyx_m) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; goto __pyx_L1;};
+  if (!__pyx_m) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1;};
   __pyx_b = PyImport_AddModule("__builtin__");
-  if (!__pyx_b) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; goto __pyx_L1;};
-  if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; goto __pyx_L1;};
-  if (__Pyx_InternStrings(__pyx_intern_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; goto __pyx_L1;};
+  if (!__pyx_b) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1;};
+  if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1;};
+  /*--- Intern code ---*/
+  __pyx_int_0 = PyInt_FromLong(0); if (unlikely(!__pyx_int_0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1;};
+  __pyx_int_1 = PyInt_FromLong(1); if (unlikely(!__pyx_int_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1;};
+  if (__Pyx_InternStrings(__pyx_intern_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1;};
+  /*--- String init code ---*/
+  /*--- Builtin init code ---*/
+  __pyx_empty_tuple = PyTuple_New(0); if (unlikely(!__pyx_empty_tuple)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  __pyx_skip_dispatch = 0;
+  /*--- Global init code ---*/
+  /*--- Function export code ---*/
+  /*--- Type init code ---*/
+  /*--- Type import code ---*/
+  /*--- Function import code ---*/
+  /*--- Execution code ---*/
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":12 */
-  __pyx_1 = __Pyx_Import(__pyx_n_struct, 0); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; goto __pyx_L1;}
-  if (PyObject_SetAttr(__pyx_m, __pyx_n_struct, __pyx_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; goto __pyx_L1;}
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":12
+ *     void * memset( void *, int, int )
+ * 
+ * import struct, sys             # <<<<<<<<<<<<<< 
+ * 
+ * from bisect import bisect
+ */
+  __pyx_1 = __Pyx_Import(__pyx_n_struct, 0); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  if (PyObject_SetAttr(__pyx_m, __pyx_n_struct, __pyx_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  Py_DECREF(__pyx_1); __pyx_1 = 0;
+  __pyx_1 = __Pyx_Import(__pyx_n_sys, 0); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  if (PyObject_SetAttr(__pyx_m, __pyx_n_sys, __pyx_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1;}
   Py_DECREF(__pyx_1); __pyx_1 = 0;
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":12 */
-  __pyx_1 = __Pyx_Import(__pyx_n_sys, 0); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; goto __pyx_L1;}
-  if (PyObject_SetAttr(__pyx_m, __pyx_n_sys, __pyx_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; goto __pyx_L1;}
-  Py_DECREF(__pyx_1); __pyx_1 = 0;
-
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":14 */
-  __pyx_1 = PyList_New(1); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; goto __pyx_L1;}
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":14
+ * import struct, sys
+ * 
+ * from bisect import bisect             # <<<<<<<<<<<<<< 
+ * 
+ * cdef char* valToNt
+ */
+  __pyx_1 = PyList_New(1); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1;}
   Py_INCREF(__pyx_n_bisect);
   PyList_SET_ITEM(__pyx_1, 0, __pyx_n_bisect);
-  __pyx_2 = __Pyx_Import(__pyx_n_bisect, __pyx_1); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; goto __pyx_L1;}
-  Py_DECREF(__pyx_1); __pyx_1 = 0;
-  __pyx_1 = PyObject_GetAttr(__pyx_2, __pyx_n_bisect); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; goto __pyx_L1;}
-  if (PyObject_SetAttr(__pyx_m, __pyx_n_bisect, __pyx_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; goto __pyx_L1;}
+  __pyx_2 = __Pyx_Import(__pyx_n_bisect, ((PyObject *)__pyx_1)); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  Py_DECREF(((PyObject *)__pyx_1)); __pyx_1 = 0;
+  __pyx_1 = PyObject_GetAttr(__pyx_2, __pyx_n_bisect); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1;}
+  if (PyObject_SetAttr(__pyx_m, __pyx_n_bisect, __pyx_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1;}
   Py_DECREF(__pyx_1); __pyx_1 = 0;
   Py_DECREF(__pyx_2); __pyx_2 = 0;
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":17 */
-  __pyx_v_7_twobit_valToNt = __pyx_k5;
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":17
+ * 
+ * cdef char* valToNt
+ * valToNt = "TCAG"             # <<<<<<<<<<<<<< 
+ * 
+ * def read( file, seq, int fragStart, int fragEnd, do_mask=False ):
+ */
+  __pyx_v_2bx_3seq_7_twobit_valToNt = __pyx_k_5;
 
-  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":19 */
-  __pyx_2 = __Pyx_GetName(__pyx_b, __pyx_n_False); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; goto __pyx_L1;}
-  __pyx_k6 = __pyx_2;
-  __pyx_2 = 0;
+  /* "/Users/james/projects/bx-python/code/trunk/lib/bx/seq/_twobit.pyx":19
+ * valToNt = "TCAG"
+ * 
+ * def read( file, seq, int fragStart, int fragEnd, do_mask=False ):             # <<<<<<<<<<<<<< 
+ *     """
+ *     Stolen directly from Jim Kent's twoBit.c
+ */
   return;
   __pyx_L1:;
   Py_XDECREF(__pyx_1);
   Py_XDECREF(__pyx_2);
-  __Pyx_AddTraceback("_twobit");
+  __Pyx_AddTraceback("bx.seq._twobit");
 }
 
 static char *__pyx_filenames[] = {
   __pyx_f = __pyx_filenames;
 }
 
+static INLINE void __Pyx_RaiseArgtupleTooLong(
+    Py_ssize_t num_expected,
+    Py_ssize_t num_found)
+{
+    const char* error_message =
+    #if PY_VERSION_HEX < 0x02050000
+        "function takes at most %d positional arguments (%d given)";
+    #else
+        "function takes at most %zd positional arguments (%zd given)";
+    #endif
+    PyErr_Format(PyExc_TypeError, error_message, num_expected, num_found);
+}
+
 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) {
     PyObject *__import__ = 0;
     PyObject *empty_list = 0;
     PyObject *py_srcfile = 0;
     PyObject *py_funcname = 0;
     PyObject *py_globals = 0;
-    PyObject *empty_tuple = 0;
     PyObject *empty_string = 0;
     PyCodeObject *py_code = 0;
     PyFrameObject *py_frame = 0;
     
     py_srcfile = PyString_FromString(__pyx_filename);
     if (!py_srcfile) goto bad;
-    py_funcname = PyString_FromString(funcname);
+    if (__pyx_clineno) {
+        py_funcname = PyString_FromFormat( "%s (%s:%u)", funcname, __pyx_cfilenm, __pyx_clineno);
+    }
+    else {
+        py_funcname = PyString_FromString(funcname);
+    }
     if (!py_funcname) goto bad;
     py_globals = PyModule_GetDict(__pyx_m);
     if (!py_globals) goto bad;
-    empty_tuple = PyTuple_New(0);
-    if (!empty_tuple) goto bad;
     empty_string = PyString_FromString("");
     if (!empty_string) goto bad;
     py_code = PyCode_New(
         0,            /*int stacksize,*/
         0,            /*int flags,*/
         empty_string, /*PyObject *code,*/
-        empty_tuple,  /*PyObject *consts,*/
-        empty_tuple,  /*PyObject *names,*/
-        empty_tuple,  /*PyObject *varnames,*/
-        empty_tuple,  /*PyObject *freevars,*/
-        empty_tuple,  /*PyObject *cellvars,*/
+        __pyx_empty_tuple,  /*PyObject *consts,*/
+        __pyx_empty_tuple,  /*PyObject *names,*/
+        __pyx_empty_tuple,  /*PyObject *varnames,*/
+        __pyx_empty_tuple,  /*PyObject *freevars,*/
+        __pyx_empty_tuple,  /*PyObject *cellvars,*/
         py_srcfile,   /*PyObject *filename,*/
         py_funcname,  /*PyObject *name,*/
         __pyx_lineno,   /*int firstlineno,*/
 bad:
     Py_XDECREF(py_srcfile);
     Py_XDECREF(py_funcname);
-    Py_XDECREF(empty_tuple);
     Py_XDECREF(empty_string);
     Py_XDECREF(py_code);
     Py_XDECREF(py_frame);
 }
+
+/* Type Conversion Functions */
+
+static INLINE Py_ssize_t __pyx_PyIndex_AsSsize_t(PyObject* b) {
+  Py_ssize_t ival;
+  PyObject* x = PyNumber_Index(b);
+  if (!x) return -1;
+  ival = PyInt_AsSsize_t(x);
+  Py_DECREF(x);
+  return ival;
+}
+
+static INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {
+   if (x == Py_True) return 1;
+   else if (x == Py_False) return 0;
+   else return PyObject_IsTrue(x);
+}
+
+static INLINE PY_LONG_LONG __pyx_PyInt_AsLongLong(PyObject* x) {
+    if (PyInt_CheckExact(x)) {
+        return PyInt_AS_LONG(x);
+    }
+    else if (PyLong_CheckExact(x)) {
+        return PyLong_AsLongLong(x);
+    }
+    else {
+        PY_LONG_LONG val;
+        PyObject* tmp = PyNumber_Int(x); if (!tmp) return (PY_LONG_LONG)-1;
+        val = __pyx_PyInt_AsLongLong(tmp);
+        Py_DECREF(tmp);
+        return val;
+    }
+}
+
+static INLINE unsigned PY_LONG_LONG __pyx_PyInt_AsUnsignedLongLong(PyObject* x) {
+    if (PyInt_CheckExact(x)) {
+        long val = PyInt_AS_LONG(x);
+        if (unlikely(val < 0)) {
+            PyErr_SetString(PyExc_TypeError, "Negative assignment to unsigned type.");
+            return (unsigned PY_LONG_LONG)-1;
+        }
+        return val;
+    }
+    else if (PyLong_CheckExact(x)) {
+        return PyLong_AsUnsignedLongLong(x);
+    }
+    else {
+        PY_LONG_LONG val;
+        PyObject* tmp = PyNumber_Int(x); if (!tmp) return (PY_LONG_LONG)-1;
+        val = __pyx_PyInt_AsUnsignedLongLong(tmp);
+        Py_DECREF(tmp);
+        return val;
+    }
+}
+
+
+static INLINE unsigned char __pyx_PyInt_unsigned_char(PyObject* x) {
+    if (sizeof(unsigned char) < sizeof(long)) {
+        long long_val = __pyx_PyInt_AsLong(x);
+        unsigned char val = (unsigned char)long_val;
+        if (unlikely((val != long_val)  || (long_val < 0))) {
+            PyErr_SetString(PyExc_OverflowError, "value too large to convert to unsigned char");
+            return (unsigned char)-1;
+        }
+        return val;
+    }
+    else {
+        return __pyx_PyInt_AsLong(x);
+    }
+}
+
+static INLINE unsigned short __pyx_PyInt_unsigned_short(PyObject* x) {
+    if (sizeof(unsigned short) < sizeof(long)) {
+        long long_val = __pyx_PyInt_AsLong(x);
+        unsigned short val = (unsigned short)long_val;
+        if (unlikely((val != long_val)  || (long_val < 0))) {
+            PyErr_SetString(PyExc_OverflowError, "value too large to convert to unsigned short");
+            return (unsigned short)-1;
+        }
+        return val;
+    }
+    else {
+        return __pyx_PyInt_AsLong(x);
+    }
+}
+
+static INLINE char __pyx_PyInt_char(PyObject* x) {
+    if (sizeof(char) < sizeof(long)) {
+        long long_val = __pyx_PyInt_AsLong(x);
+        char val = (char)long_val;
+        if (unlikely((val != long_val) )) {
+            PyErr_SetString(PyExc_OverflowError, "value too large to convert to char");
+            return (char)-1;
+        }
+        return val;
+    }
+    else {
+        return __pyx_PyInt_AsLong(x);
+    }
+}
+
+static INLINE short __pyx_PyInt_short(PyObject* x) {
+    if (sizeof(short) < sizeof(long)) {
+        long long_val = __pyx_PyInt_AsLong(x);
+        short val = (short)long_val;
+        if (unlikely((val != long_val) )) {
+            PyErr_SetString(PyExc_OverflowError, "value too large to convert to short");
+            return (short)-1;
+        }
+        return val;
+    }
+    else {
+        return __pyx_PyInt_AsLong(x);
+    }
+}
+
+static INLINE int __pyx_PyInt_int(PyObject* x) {
+    if (sizeof(int) < sizeof(long)) {
+        long long_val = __pyx_PyInt_AsLong(x);
+        int val = (int)long_val;
+        if (unlikely((val != long_val) )) {
+            PyErr_SetString(PyExc_OverflowError, "value too large to convert to int");
+            return (int)-1;
+        }
+        return val;
+    }
+    else {
+        return __pyx_PyInt_AsLong(x);
+    }
+}
+
+static INLINE long __pyx_PyInt_long(PyObject* x) {
+    if (sizeof(long) < sizeof(long)) {
+        long long_val = __pyx_PyInt_AsLong(x);
+        long val = (long)long_val;
+        if (unlikely((val != long_val) )) {
+            PyErr_SetString(PyExc_OverflowError, "value too large to convert to long");
+            return (long)-1;
+        }
+        return val;
+    }
+    else {
+        return __pyx_PyInt_AsLong(x);
+    }
+}
+
+static INLINE signed char __pyx_PyInt_signed_char(PyObject* x) {
+    if (sizeof(signed char) < sizeof(long)) {
+        long long_val = __pyx_PyInt_AsLong(x);
+        signed char val = (signed char)long_val;
+        if (unlikely((val != long_val) )) {
+            PyErr_SetString(PyExc_OverflowError, "value too large to convert to signed char");
+            return (signed char)-1;
+        }
+        return val;
+    }
+    else {
+        return __pyx_PyInt_AsLong(x);
+    }
+}
+
+static INLINE signed short __pyx_PyInt_signed_short(PyObject* x) {
+    if (sizeof(signed short) < sizeof(long)) {
+        long long_val = __pyx_PyInt_AsLong(x);
+        signed short val = (signed short)long_val;
+        if (unlikely((val != long_val) )) {
+            PyErr_SetString(PyExc_OverflowError, "value too large to convert to signed short");
+            return (signed short)-1;
+        }
+        return val;
+    }
+    else {
+        return __pyx_PyInt_AsLong(x);
+    }
+}
+
+static INLINE signed int __pyx_PyInt_signed_int(PyObject* x) {
+    if (sizeof(signed int) < sizeof(long)) {
+        long long_val = __pyx_PyInt_AsLong(x);
+        signed int val = (signed int)long_val;
+        if (unlikely((val != long_val) )) {
+            PyErr_SetString(PyExc_OverflowError, "value too large to convert to signed int");
+            return (signed int)-1;
+        }
+        return val;
+    }
+    else {
+        return __pyx_PyInt_AsLong(x);
+    }
+}
+
+static INLINE signed long __pyx_PyInt_signed_long(PyObject* x) {
+    if (sizeof(signed long) < sizeof(long)) {
+        long long_val = __pyx_PyInt_AsLong(x);
+        signed long val = (signed long)long_val;
+        if (unlikely((val != long_val) )) {
+            PyErr_SetString(PyExc_OverflowError, "value too large to convert to signed long");
+            return (signed long)-1;
+        }
+        return val;
+    }
+    else {
+        return __pyx_PyInt_AsLong(x);
+    }
+}
+
+static INLINE long double __pyx_PyInt_long_double(PyObject* x) {
+    if (sizeof(long double) < sizeof(long)) {
+        long long_val = __pyx_PyInt_AsLong(x);
+        long double val = (long double)long_val;
+        if (unlikely((val != long_val) )) {
+            PyErr_SetString(PyExc_OverflowError, "value too large to convert to long double");
+            return (long double)-1;
+        }
+        return val;
+    }
+    else {
+        return __pyx_PyInt_AsLong(x);
+    }
+}

lib/bx/seq/_twobit.pyx

 cdef char* valToNt
 valToNt = "TCAG"
 
-def read( file, seq, int fragStart, int fragEnd ):
+def read( file, seq, int fragStart, int fragEnd, do_mask=False ):
     """
     Stolen directly from Jim Kent's twoBit.c
     """
             if (s < e):
                 memset( dna + s - fragStart, 'N'[0], e - s)
     # Mask
-    m_block_count = len( seq.masked_block_starts )
-    if m_block_count > 0:
-        start_ix = bisect( seq.masked_block_starts, fragStart ) - 1
-        if start_ix < 0: start_ix = 0    
-        for i from start_ix <= i < m_block_count:
-            s = seq.masked_block_starts[i];
-            e = s + seq.masked_block_sizes[i];
-            if (s >= fragEnd):
-                break
-            if (s < fragStart):
-               s = fragStart
-            if (e > fragEnd):
-               e = fragEnd
-            if (s < e):
-                for j from s <= j < e:
-                    dna[j-fragStart] = tolower( dna[j-fragStart] )
+    if do_mask:
+        m_block_count = len( seq.masked_block_starts )
+        if m_block_count > 0:
+            start_ix = bisect( seq.masked_block_starts, fragStart ) - 1
+            if start_ix < 0: start_ix = 0    
+            for i from start_ix <= i < m_block_count:
+                s = seq.masked_block_starts[i];
+                e = s + seq.masked_block_sizes[i];
+                if (s >= fragEnd):
+                    break
+                if (s < fragStart):
+                   s = fragStart
+                if (e > fragEnd):
+                   e = fragEnd
+                if (s < e):
+                    for j from s <= j < e:
+                        dna[j-fragStart] = tolower( dna[j-fragStart] )
     return dna_py

lib/bx/seq/twobit.py

         assert stride == 1, "Striding in slices not supported"
         if stop - start < 1:
             return ""
-        return _twobit.read( self.tbf.file, self, start, stop )
+        return _twobit.read( self.tbf.file, self, start, stop, self.tbf.do_mask  )
         
     def __len__( self ):
         return self.size
         if out_size < 1:
             raise Exception( "end before start (%s,%s)" % ( start,end ) )
         # Find position of packed portion
-        dna = _twobit.read( self.tbf.file, self, start, end )
+        dna = _twobit.read( self.tbf.file, self, start, end, self.tbf.do_mask )
         # Return
         return dna
         
 class TwoBitFile( DictMixin ):
-    def __init__( self, file ):
+    def __init__( self, file, do_mask=True ):
+        self.do_mask = do_mask
         # Read magic and determine byte order
         self.byte_order = ">"
         magic = unpack( ">L", file.read( TWOBIT_MAGIC_SIZE ) )[0]